2013-04-24 66 views
1

想知道如何去創建SMS廣播接收器而不使用清單文件。因此,舉例來說,這是我目前的清單文件行:沒有清單的SMS廣播接收器

​​

,它的工作很好,但我希望能夠刪除行,我的服務像內登記自己的廣播接收器:

registerReceiver(new SMSListener() , [?????]); 

但我無法找到要使用的Intent過濾器,因爲SMS_RECEIVED是Telephony包的一部分。所以問題是:什麼是意圖過濾器?

回答

3

在你的java文件中創建IntentFilter,然後registerReceiver(Receiver, IntentFilter)

行動將是android.provider.Telephony.SMS_RECEIVED爲字符串。

IntentFilter i = new IntentFilter("android.provider.Telephony.SMS_RECEIVED"); 
registerReceiver(new SMSListener() , i); 
+0

完美的,沒有意識到我可能只是轉換那些直接以這樣的字符串,謝謝。 – 2013-04-24 04:08:56

0
mSmsReceiver = new SMSreceiver(); 
    IntentFilter filter = new IntentFilter(); 
    filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); 
    filter.addAction(SMS_RECEIVE_ACTION); // SMS 
    filter.addAction(WAP_PUSH_RECEIVED_ACTION); // MMS 
    this.registerReceiver(mSmsReceiver, filter); 

    class SMSreceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (!getSMSBlockEnable()) { 
      return; 
     } 

     if (TextUtils.equals(intent.getAction(), SMS_RECEIVE_ACTION)) { 

      Bundle bundle = intent.getExtras(); 
      if (bundle == null) { 
       return; 
      } 
      Object[] messages = (Object[])bundle.get("pdus"); 

      SmsMessage smsHeader = SmsMessage.createFromPdu((byte[])messages[0]); 
      if (smsHeader != null) { 
       mReceiveNumber = smsHeader.getOriginatingAddress(); 
       if(TextUtils.isEmpty(mReceiveNumber)) { 
        mReceiveNumber = INVALID_NUMBER; 
       } 
      } 

      for (Object message : messages) { 
       SmsMessage sms = SmsMessage.createFromPdu((byte[])message); 
        if (sms != null) { 
        body.append(sms.getDisplayMessageBody()); 
        } 
      } 
      mReceiveBody = body.toString(); 
     } 
    } 
} 
+0

不幫我,因爲在該代碼中沒有定義常量SMS_RECEIVE_ACTION:S – 2013-04-24 04:09:27

+1

這個答案可能會幫助其他用戶,該常量在Telephony.Sms.Intents.SMS_RECEIVED_ACTION – 2017-02-07 18:13:47