注册广播接收器对于接收到的SMS(Register BroadcastReceiver For In

2019-08-21 01:47发布

如何注册广播接收器,用于阻止传入短信

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

Answer 1:

尝试以下方法:

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

希望这可以帮助。



Answer 2:

您应该检查权限

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);
}
    }


Answer 3:

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

    <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();
    }
}                


Answer 4:

声明在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>

希望这有助于亚龙



文章来源: Register BroadcastReceiver For Incoming SMS