1

想知道如何在不使用 Manifest 文件的情况下创建 SMS 广播接收器。例如,这是我当前的清单文件行:

<receiver android:name="com.paradopolis.randomnotifications.SMSListener">
        <intent-filter>
          <action android:name="android.provider.Telephony.SMS_RECEIVED" />
       </intent-filter>
</receiver>

它工作正常,但我希望能够删除线路并在我的服务中注册我的广播接收器,例如:

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

但我找不到要使用的 Intent 过滤器,因为 SMS_RECEIVED 是 Telephony 包的一部分。所以问题是:那个意图过滤器是什么?

4

2 回答 2

7

IntentFilter在你的java文件中创建,然后registerReceiver(Receiver, IntentFilter)

该操作将android.provider.Telephony.SMS_RECEIVED作为一个字符串。

IntentFilter i = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(new SMSListener() , i);
于 2013-04-24T02:30:52.323 回答
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();
        }
    }
}
于 2013-04-24T03:26:57.483 回答