9

如何注册 BroadcastReceiver 以阻止传入的 SMS

    IntentFilter intentFilter = new IntentFilter(Intent."what should be here");
     ctx.registerReceiver(outgoingReceiver, intentFilter);
4

4 回答 4

13

尝试以下操作:

intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");

希望这可以帮助。

于 2013-04-09T20:47:38.880 回答
4

你应该检查权限

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);
}
    }
于 2017-04-17T06:41:52.763 回答
1

在您的清单中注册一个具有最高优先级的接收器:

    <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();
    }
}                
于 2013-04-09T20:49:14.247 回答
1

在你的 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>

希望这会有所帮助,亚龙

于 2013-04-09T20:53:06.423 回答