我有一個廣播接收器SMSApp,其中onReceive從意圖讀取bundleExtras。在這裏,我如何從bundleExtras中讀取電話號碼?現在Android:如何從傳入的短信中獲取電話號碼?
if(intent.getAction().equals(SMS_RECEIVED))
{
Bundle bundleExtras = intent.getExtras();
if (bundleExtras != null)
{
}
}
我有一個廣播接收器SMSApp,其中onReceive從意圖讀取bundleExtras。在這裏,我如何從bundleExtras中讀取電話號碼?現在Android:如何從傳入的短信中獲取電話號碼?
if(intent.getAction().equals(SMS_RECEIVED))
{
Bundle bundleExtras = intent.getExtras();
if (bundleExtras != null)
{
}
}
Bundle bundle = intent.getExtras();
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]);
}
,messages[0].getOriginatingAddress()
是地址和messages[0].getMessageBody()
是消息主體。
謝謝你的工作 – 2012-02-21 13:35:40
這是一個詳盡的「如何發送和接收短信」,你應該檢查它的鏈接,它是完整的,充滿例子:
http://mobiforge.com/developing/story/sms-messaging-android
也是用這個link,你可以這樣做:
if (bundle != null) {
SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent);
for (int i = 0; i < messages.length; i++) {
SmsMessage message = messages[i];
buf.append("Received SMS from ");
buf.append(message.getDisplayOriginatingAddress());
buf.append(" - ");
buf.append(message.getDisplayMessageBody());
}
}
這對我不起作用,編譯器無法識別Telephony類。有以下問題:Telephony.Sms.Intents.getMessagesFromIntent(intent); – 2012-02-21 13:29:15
教程在http://ukitech.blogspot.com/2014/11/android-sms-app.html
OUTPUT:
個SmsReceiver.processReceivedSms:從+ 1650815xxxx
SmsReceiver.processReceivedSms SMS:SMS體試驗4
/** * 創建者UKI上14年11月22日。 */
公共類SmsReceiver擴展廣播接收器{
@Override
public void onReceive(Context context, Intent intent) {
final String tag = TAG + ".onReceive";
Bundle bundle = intent.getExtras();
if (bundle == null) {
Log.w(tag, "BroadcastReceiver failed, no intent data to process.");
return;
}
if (intent.getAction().equals(SMS_RECEIVED)) {
Log.d(tag, "SMS_RECEIVED");
String smsOriginatingAddress, smsDisplayMessage;
// You have to CHOOSE which code snippet to use NEW (KitKat+), or legacy
// Please comment out the for{} you don't want to use.
// API level 19 (KitKat 4.4) getMessagesFromIntent
for (SmsMessage message : Telephony.Sms.Intents.
getMessagesFromIntent(intent)) {
Log.d(tag, "KitKat or newer");
if (message == null) {
Log.e(tag, "SMS message is null -- ABORT");
break;
}
smsOriginatingAddress = message.getDisplayOriginatingAddress();
//see getMessageBody();
smsDisplayMessage = message.getDisplayMessageBody();
processReceivedSms(smsOriginatingAddress, smsDisplayMessage);
}
// Processing SMS messages the OLD way, before KitKat,
// this WILL work on KitKat or newer Android
// PDU is a 「protocol data unit」, which is the industry
// format for an SMS message
Object[] data = (Object[]) bundle.get("pdus");
for (Object pdu : data) {
Log.d(tag, "legacy SMS implementation (before KitKat)");
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdu);
if (message == null) {
Log.e(tag, "SMS message is null -- ABORT");
break;
}
smsOriginatingAddress = message.getDisplayOriginatingAddress();
// see getMessageBody();
smsDisplayMessage = message.getDisplayMessageBody();
processReceivedSms(smsOriginatingAddress, smsDisplayMessage);
}
} // onReceive method
可能重複http://stackoverflow.com/questions/5692354/in-android-how-should-i-get-phone-number-of -sms-sender – 2012-02-21 13:22:02
@sergeybenner謝謝 – 2012-02-21 13:31:40