想知道如何在不使用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包的一部分。所以问题是:那个意图过滤器是什么?
答案 0 :(得分:5)
在您的java文件中创建IntentFilter
,然后registerReceiver(Receiver, IntentFilter)
该操作将android.provider.Telephony.SMS_RECEIVED
作为字符串。
IntentFilter i = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(new SMSListener() , i);
答案 1 :(得分: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();
}
}
}