问题
I wrote send and receive sms program sucsessfully but I want to that checked receiver phone number in onReceive method. how can i get the sender phone number from a received sms in android? I wrote this code but it dosent worked!!! please check it and help me.
public class SmsReceiver extends BroadcastReceiver {
public String str = "";
@Override
public void onReceive(Context context, Intent intent) {
// ---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
//for get sms from special number===============================
String msg_from = msgs[i].getOriginatingAddress();
Log.v("msg_from >>",msg_from);
if(msg_from.equals("08522215"))
{
//===============================
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
}
// ---display the new SMS message---
// Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
Intent act = new Intent(context, MainActivity.class);
act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
act.putExtra("message", str);
context.startActivity(act);
}
abortBroadcast();
}
}
manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<activity android:name=".SMS"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:label="@string/app_name"/>
<receiver
android:name="com.example.sms.SmsReceiver"
class="com.example.sms.SmsReceiver" >
<intent-filter android:priority="100" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
回答1:
In the bundle object itself the sender number is received.
回答2:
Write this in the broadcast receiver
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++)
{
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
String receivedMessage = smsMessage[0].getMessageBody().toString().toUpperCase();
String originatingAddress = smsMessage[0].getOriginatingAddress();
originatingAddress = (originatingAddress!=null && originatingAddress.length()>3)?(originatingAddress.substring(3)):("") ;
The Originating address above is actually the sender number...
EDIT
you probably used
android.telephony.gsm.SmsMessage
that is deprecated. Use this instead:
android.telephony.SmsMessage
回答3:
Try the following code
Don't forget to give SMS receive permission in manifest file
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
public class SmsReceiver extends BroadcastReceiver{
private Intent intent;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
/* ************** BIG COMMENT STARTS *******
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
SmsMessage[] phonenum =null;//
String str = "";
String PhoneNUMBER =""; //
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---retrieve the SMS senders number ---
phonenum = new SmsMessage[pdus.length];
for (int i = 0; i < phonenum.length; i++) {
phonenum[i]=SmsMessage.createFromPdu((byte[])pdus[i]);
PhoneNUMBER += phonenum[i].getOriginatingAddress();
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
Toast.makeText(context, PhoneNUMBER, Toast.LENGTH_LONG).show();
}
来源:https://stackoverflow.com/questions/13098472/any-way-to-get-the-sender-phone-number-from-a-received-sms-on-android