请任何人给我下载的代码或任何有用的链接,以便在接收消息时获取 SMS 发件人的电话号码。我尝试了很多。
问问题
716 次
2 回答
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());
}
}
}
}
希望它会起作用,或引导您找到解决方案
于 2012-12-11T10:11:38.753 回答
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();
}
}
于 2012-12-11T10:53:02.900 回答