广播的优先级和处理方法

3 篇文章 0 订阅
2 篇文章 0 订阅

1.无序广播发送方发出后几乎同时到达多个广播接收者处并且无法终止广播继续传播使用Context.sendBroadcast(intent);

2.有序广播广播接收者需要提前设置优先级优先级高的先接收到广播而且能终止广播(abortBroadcast())使用Context.sendOrderedBroadcast(intent);有序广播可以设定接收的优先级并在接收后删掉这个广播传送XML中设定receiver的优先级

<receiverandroid:name=".smsReceiver">

<intent-filterandroid:priority="1000">

<actionandroid:name="android.provider.Telephony.SMS_RECEIVED"/>

</intent-filter>

</receiver>

优先级别声明在intent-filter元素的android:priority属性中数越大优先级别越高最大值

2147483647优先级别也可以调用IntentFilter对象的setPriority()进行设置

有序广播的接收者可以终止广播Intent的传播广播Intent的传播一旦终止后面的接收者就无法接收到广播使用abortBroadcast();即可这种用法可以用在短信拦截或者位置跟踪应用中比如收到某个特定SMS信息处理它之后就把广播终止防止其他应用收到

总体来说对于接收同一个广播在相同优先级的情况下动态注册优先级别高于静态注册
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@SuppressLint("NewApi") public class MainActivity extends Activity { SmsReceiver myReceiver; Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { test(); } }); } public void test(){ Cursor cursor = null; String defaultSmsApp = Telephony.Sms.getDefaultSmsPackage(this); Intent intent = new Intent(Sms.Intents.ACTION_CHANGE_DEFAULT); intent.putExtra(Sms.Intents.EXTRA_PACKAGE_NAME, this.getPackageName()); startActivity(intent); try { cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), new String[] { "_id", "address", "read" }, "read = ? ", new String[] {"0" }, "date desc"); if (cursor != null) { ContentValues values = new ContentValues(); values.put("read", "1"); for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { Log.v("cky", "" + cursor.getInt(cursor.getColumnIndex("_id")) + " , " + cursor.getString(cursor.getColumnIndex("address"))); int res = getContentResolver().update(Uri.parse("content://sms/inbox"), values, "_id=?", new String[] { "" + cursor.getInt(cursor.getColumnIndex("_id")) }); Log.i("cky","geng xin = "+res); } } intent = new Intent(Sms.Intents.ACTION_CHANGE_DEFAULT); intent.putExtra(Sms.Intents.EXTRA_PACKAGE_NAME, defaultSmsApp); startActivity(intent); } catch (Exception e) { e.printStackTrace(); } finally { if (cursor != null) { cursor.close(); cursor = null; } } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值