Android之定向广播
2016-07-07 18:52
357 查看
Android中当多个应用都接收同一个广播时,会导致很多系统进程同时开启,这会导致系统卡顿。有了ssp我们可以定向的接收或发送某一特定广播达到优化系统的效果。
接收或发送定向广播需要用到android:ssp属性用于匹配URI,ssp代码“scheme-specific part”,意思是代表的东西都在scheme之后出现,如URI是“http://example.com.a”,可以分成scheme部分"http"和ssp部分"//example.com.a"。这里需要特别注意例子中的ssp部分是包含"//"的。
ssp相关:
android:ssp全匹配
android:sspPrefix前缀匹配
android:sspPattern模式匹配
1.接收安装某一特定应用的系统广播
注册方式一:
注册方式二:
2.定向发送和接收自定义广播
2.1发送
2.2接收
上面注册广播时使用多进程是为了模拟跨应用接收定向广播的。
接收或发送定向广播需要用到android:ssp属性用于匹配URI,ssp代码“scheme-specific part”,意思是代表的东西都在scheme之后出现,如URI是“http://example.com.a”,可以分成scheme部分"http"和ssp部分"//example.com.a"。这里需要特别注意例子中的ssp部分是包含"//"的。
ssp相关:
android:ssp全匹配
android:sspPrefix前缀匹配
android:sspPattern模式匹配
1.接收安装某一特定应用的系统广播
注册方式一:
<receiver android:name="com.example.sspreceiver.PackageAddReceiver"> <intent-filter > <action android:name="android.intent.action.PACKAGE_ADDED"/> <data android:scheme="package" android:ssp="com.tencent.mm"/> </intent-filter> </receiver>
注册方式二:
IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_PACKAGE_ADDED); filter.addDataScheme("package"); // PatternMatcher.PATTERN_LITERAL相当于ssp // PatternMatcher.PATTERN_PREFIX相当于sspPrefix // atternMatcher.PATTERN_SIMPLE_GLOB相当于sspPattern filter.addDataSchemeSpecificPart("com.tencent.mm", PatternMatcher.PATTERN_LITERAL);
2.定向发送和接收自定义广播
2.1发送
String RECEIVE_MSG_ACTION = "com.example.sspreceiver.msg_action"; Button button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction(RECEIVE_MSG_ACTION); Uri data = Uri.parse("msg://com.example.ssp1"); intent.setData(data); MainActivity.this.sendBroadcast(intent); } }); Button button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(RECEIVE_MSG_ACTION, Uri .parse("msg:com.example.ssp2")); MainActivity.this.sendBroadcast(intent); } });
2.2接收
<receiver android:name="com.example.sspreceiver.Ssp1Receiver" android:process=":ssp1"> <intent-filter > <action android:name="com.example.sspreceiver.msg_action"/> <data android:scheme="msg" android:sspPrefix="//com.example.ssp1"/> </intent-filter> </receiver> <receiver android:name="com.example.sspreceiver.Ssp2Receiver" android:process=":ssp2"> <intent-filter > <action android:name="com.example.sspreceiver.msg_action"/> <data android:scheme="msg" android:ssp="com.example.ssp2"/> </intent-filter> </receiver>
上面注册广播时使用多进程是为了模拟跨应用接收定向广播的。
相关文章推荐
- Android异常解决--A WebView method was called on thread 'JavaBridge'. All WebView methods must be called
- 浅谈Android开发中内存泄露与优化-------其一
- 随记
- Android 将View 转化为bitmap 图片
- android之Activity的切换动画(进出动画)
- android时间组件TimePicker与DatePicker
- Android事件分发
- Android 使用ZXing扫描在部分手机上扫描变形解决办法
- 通过Button按钮调节亮度
- 【Android实战】EventBus 更少的代码 更好的体验
- 5分钟搞定android混淆
- x264 android移植
- Android 通过JNI实现守护进程,使Service服务不被杀死
- 【Android】SQLite 数据库基本操作
- Android实战技巧之十二:Android Studio导入第三方类库、jar包和so库
- android 轮播图
- android判断应用是否有某个权限
- java.lang.Class Cast Exception: android.widget.HeaderViewListAdapter
- Android 中线程池的理解
- Android AsyncTask 的工作原理