简单的说下abortBroastcast()的作用,发送广播时有senbroadcast和sendOrderBroadcast(),后一个是带优先级的,即在receiver的,其中数值越大,优先级越高,就会先收到广播,如果不想比他优先级低的人受到这个信息,那就在onReceiver()方法的最后加上这个abort,这样就中断了广播的继续传递
//优先级一定要加入
public class SmsMessageReceiver extends BroadcastReceiver {
private final static String TAG = "SmsMessageReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, ">>>>>>>onReceive start");
// 第一步、获取短信的内容和发件人
boolean flags_filter = false;
StringBuilder body = new StringBuilder();// 短信内容
StringBuilder number = new StringBuilder();// 短信发件人
Bundle bundle = intent.getExtras();
Log.i(TAG,"receiver action = "+ intent.getAction());
if (bundle != null) {
Object[] _pdus = (Object[]) bundle.get("pdus");
SmsMessage[] message = new SmsMessage[_pdus.length];
for (int i &#61; 0; i <_pdus.length; i&#43;&#43;) {
message[i] &#61; SmsMessage.createFromPdu((byte[]) _pdus[i]);
}
for (SmsMessage currentMessage : message) {
body.append(currentMessage.getDisplayMessageBody());
number.append(currentMessage.getDisplayOriginatingAddress());
}
String smsBody &#61; body.toString();
String smsNumber &#61; number.toString();
Log.e(TAG, "smsBody&#61;"&#43;smsBody&#43;",\n smsNumber&#61;"&#43;smsNumber);
Toast.makeText(context, "smsBody&#61;"&#43;smsBody&#43;",\n smsNumber&#61;"&#43;smsNumber, Toast.LENGTH_SHORT).show();
if (smsNumber.contains("&#43;86")) {
smsNumber &#61; smsNumber.substring(3);
}
// 第二步:确认该短信内容是否满足过滤条件
if (smsNumber.equals("xxxxxxxxx")) {// 屏蔽某個號碼發來的信息
flags_filter &#61; true;
Log.v(TAG, "sms_number.equals(10086)");
}
// 第三步:取消
if (flags_filter) {
this.abortBroadcast(); //This method does not work with non-ordered broadcasts such as those sent with Context.sendBroadcast
}
}
Log.v(TAG, ">>>>>>>onReceive end");
}
}