如何註冊廣播接收器,用於阻止傳入的短信註冊廣播接收器對於接收到的SMS
IntentFilter intentFilter = new IntentFilter(Intent."what should be here");
ctx.registerReceiver(outgoingReceiver, intentFilter);
如何註冊廣播接收器,用於阻止傳入的短信註冊廣播接收器對於接收到的SMS
IntentFilter intentFilter = new IntentFilter(Intent."what should be here");
ctx.registerReceiver(outgoingReceiver, intentFilter);
嘗試以下操作:
intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
希望這有助於。
在您的清單註冊一個接收器,具有最高的優先級:
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="9999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
創建廣播接收器類:
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
}
}
實現的onReceive方法:
Bundle bundle = intent.getExtras();
if (bundle != null && bundle.containsKey("pdus")) {
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage sms = SmsMessage.createFromPdu((byte[]) pdus[0]);
String senderNumber = sms.getOriginatingAddress();
if(senderIsInBlackList(senderNumber)) {
abortBroadcast();
}
}
聲明接收器在你的AndroidManifest。 xml:
<receiver
android:name="com.example.YourSmsReceiver"
android:exported="true" >
<intent-filter android:priority="1000" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
希望這有助於亞龍
您應該檢查許可
if (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.RECEIVE_SMS}, 0);
} else {
// register sms receiver
IntentFilter filter = new IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION);
registerReceiver(smsReceiver, filter);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) {
} else {
// register sms receiver
IntentFilter filter = new IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION);
registerReceiver(smsReceiver, filter);
}
}
是不是優先級應該是0?據谷歌博客0意味着「運行所有其他人」 – 2014-11-05 00:07:23
他/她想阻止一些傳入的消息,所以這個應用程序應該有最高的可用優先級。 – 2014-11-05 05:23:34