1
你好,我已經實現了廣播接收器完全一樣在這個崗位 Android – Listen For Incoming SMS Messages廣播接收器收聽短信不能正常工作
但由於某些原因,當我嘗試從一個AVD將消息發送到與應用乳寧第二AVD,它並不甚至打印「收到的消息」文本記錄。
清單:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<application>
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsListener">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
廣播接收器
public class SmsListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("SmsListener", "Message received");
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Log.d("SmsListener", "Message received");
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
String msg_from;
if (bundle != null){
//---retrieve the SMS message received---
try{
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]);
msg_from = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();
Log.d("SmsListener", "Message: "+msgBody);
}
} catch(Exception e) {
Log.d("Exception caught", e.getMessage());
}
}
}
}
}
誰能解釋一下問題出在哪裏?