代码如何从Android中收到的短信获取短信发送者的电话号码

时间:2012-12-11 09:59:44

标签: android sms broadcastreceiver sms-gateway phone-number

任何人都可以在收到邮件时向我提供下载的代码或任何有用的链接,以获取短信发送者的电话号码。 我尝试了很多。

2 个答案:

答案 0 :(得分:0)

试试这个,此处messages[0].getOriginatingAddress()将返回短信发送者的电话号码

public void onReceive(Context context, Intent intent) {
          Log.i(TAG, "Intent recieved: " + intent.getAction());

             if (intent.getAction() == SMS_RECEIVED) {
                 Bundle bundle = intent.getExtras();
                 if (bundle != null) {
                     Object[] pdus = (Object[])bundle.get("pdus");
                     final SmsMessage[] messages = new SmsMessage[pdus.length];
                     for (int i = 0; i < pdus.length; i++) {
                         messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                     }
                     if (messages.length > -1) {
                         Log.i(TAG, "Message recieved: " + messages[0].getMessageBody());
                         Log.i(TAG, "Message recieved: " + messages[0].getOriginatingAddress());
                     }
                 }
             }
        }

希望它能够奏效,或引导您找到解决方案

答案 1 :(得分:0)

@Override
public void onReceive(Context ctx, Intent intent) {
    //Log.d(Params.LOG_APP, "INCOMING SMS....");



    Bundle extras = intent.getExtras();

    if (extras == null)
        return ;

    Object[] dataArr = (Object[]) extras.get("pdus");
    //Log.d(Params.LOG_APP, "dataArr ["+dataArr+"]");



    for (Object data: dataArr) {
        SmsMessage sms = SmsMessage.createFromPdu((byte[]) data);
        //Log.d(Params.LOG_APP, "SMS ["+sms+"]");
        String body = sms.getMessageBody();
        String sender = sms.getOriginatingAddress();

        long time = sms.getTimestampMillis();

    }

}