2016-02-10 145 views
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()); 
      } 
     } 
    } 
} 
} 

誰能解釋一下問題出在哪裏?

回答

0

從KitKat入手短信廣播攔截開始有點變化。看看這個清單,其中涵蓋了售前奇巧和奇巧版本:

<!-- SMS receiver --> 
     <receiver 
      android:name=".SmsReceiver" 
      android:enabled="true" 
      android:exported="true" 
      android:permission="android.permission.BROADCAST_SMS" > 
      <intent-filter android:priority="2147483647" > <!-- 999 is highest system priority, so it's hack 2147483647 --> 
       <action android:name="android.provider.Telephony.SMS_RECEIVED" /> <!-- pre kitkat action --> 
       <action android:name="android.provider.Telephony.SMS_DELIVER" /> <!-- kitkat action --> 
      </intent-filter> 
     </receiver> 

因此根據Android版本,你有不同的方式攔截:

@Override 
    public void onReceive(Context context, Intent intent) { 
     //<action android:name="android.provider.Telephony.SMS_RECEIVED" /> <!-- pre kitkat action --> 
     //<action android:name="android.provider.Telephony.SMS_DELIVER" /> <!-- kitkat action --> 

     //if it is kitkat - ignore pre kitkat action 
     if(intent.getAction().equalsIgnoreCase("android.provider.Telephony.SMS_RECEIVED") && android.os.Build.VERSION.SDK_INT >= 19) 
      return; 
     //if it is not kitkat - ignore kitkat action 
     if(intent.getAction().equalsIgnoreCase("android.provider.Telephony.SMS_DELIVER") && android.os.Build.VERSION.SDK_INT < 19) 
      return; 
    //blah-blah 
}