Android编程权威指南学习之broadcast intent(二)

2.3.3 私有权限的使用背景:
系统中的任何应用均可监听并触发我们的receiver,通常这种情况是不希望发生的。
方法一:在manifest配置文件里给receiver标签添加一个android:exported=”false”属性,声明它仅限应用内部使用。
方法二:在AndroidManifest.xml中添加permission标签来完成。

<permission android:name="com.bignerdranch.android.photogallery.PRIVATE"android:protectionLevel="signature" /><uses-permission android:name="com.bignerdranch.android.photogallery.PRIVATE" />

发送的Broadcast intent:

public static final String PERM_PRIVATE
="com.bignerdranch.android.photogallery.PRIVATE";sendBroadcast(new Intent(ACTION_SHOW_NOTIFICATION), PERM_PRIVATE);

接收的Broadcast receiver,在registerReceiver()方法传入自定义权限就能解决该问题。

IntentFilter filter = new IntentFilter(PollService.ACTION_SHOW_NOTIFICATION);getActivity().registerReceiver(mOnShowNotification, filter,PollService.PERM_PRIVATE, null)

注意:自定义权限必须指定android:protectionLevel属性值,Android根据protectionLevel属性值确定自定义权限的使用方式。

2.3.4 有序broadcast的使用
一般broadcast intent可同时被所有人接收。事实上, onReceive(…)方法是在主线程上调用的,所以receiver并没有同步并发运行。因而,不可能指望它们按照某种顺序依次运行,或知道它们什么时候全部结束运行。结果就是,无论是broadcast receiver之间要通信,还是intent发送者要从receiver接收信息,都会很麻烦。
为解决问题,可使用有序broadcast intent实现双向通信。有序broadcast允许多个broadcast receiver依序处理broadcast intent。另外,通过传入一个名为result receiver的特别broadcast receiver,有序broadcast还支持让broadcast发送者接收broadcast接收者的返回结果。

  private void showBackgroundNotification(int requestCode, Notification notification) { 
   Intent i = new Intent(ACTION_SHOW_NOTIFICATION);
   i.putExtra(REQUEST_CODE, requestCode); 
   i.putExtra(NOTIFICATION, notification);
   sendOrderedBroadcast(i, PERM_PRIVATE, null, null,Activity.RESULT_OK, null, null);
  } 

除了在 sendBroadcast(Intent,String) 方法中使用的参数外, Context.sendOrdered-Broadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle) 方法还有另外五个参数,依次为:一个result receiver,一个支持result receiver运行的 Handler ,结果代码初始值,结果数据以及有序broadcast的结果附加内容。
result receiver比较特殊,只有在所有有序broadcast intent接收者结束运行后,它才开始运行。虽然有时能使用result receiver接收broadcast和发布通知对象,但此处行不通。目标broadcast intent通常是在 PollService 对象消亡之前发出的,这意味着broadcast receiver很可能也不存在了。
因此,最终的broadcast receiver应该是个standalone receiver。而且,无论如何都要保证它在其他动态登记的receiver之后运行。

public class NotificationReceiver extends BroadcastReceiver { 
   private static final String TAG = "NotificationReceiver"; 
   @Override public void onReceive(Context c, Intent i) { 
   Log.i(TAG, "received result: " + getResultCode()); 
   if (getResultCode() != Activity.RESULT_OK) { 
   // A foreground activity cancelled the broadcast return; 
   } 
   int requestCode = i.getIntExtra(PollService.REQUEST_CODE, 0); 
   Notification notification = (Notification)i.getParcelableExtra(PollService.NOTIFICATION); 
   NotificationManagerCompat notificationManager = NotificationManagerCompat.from(c);     
   notificationManager.notify(requestCode, notification); 
   }
 } 
对改进行登记:
 <receiver android:name=".NotificationReceiver" android:exported="false"> <intent-filter android:priority="-999"> <action android:name="com.bignerdranch.android.photogallery.SHOW_NOTIFICATION" /> 
 </intent-filter> </receiver>

3、receiver与长时运行任务 使用broadcast intent触发一个长时运行任务。
两种方法:
1)将任务交给服务处理,然后通过broadcast receiver启动服务。这也是首推方式。
2)使用BroadcastReceiver.goAsync()方法。

4.深入学习:本地事件
背景:需要一个仅在应用内使用的消息事件广播。
事件总线(EventBus)设计思路:提供一个应用内的部件可以订阅的共享总线或数据流。事件一旦发布到总线上,各订阅部件就会被激活并执行相应的回调代码。
EventBus的使用:要在应用中使用EventBus,首先需要在项目中添加依赖库。然后,就可以定义事件类了(如果需要传送数据,可以向事件里添加数据字段):
public class NewFriendAddedEvent { }
在应用的任何地方,都可以把消息事件发布到总线上:
EventBus eventBus = EventBus.getDefault();eventBus.post(new NewFriendAddedEvent());
在总线上登记监听,应用的其他部分也可以订阅接收事件消息。通常,activity或fragment的登记和撤销登记都是在相应的生命周期方法中
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
mEventBus.register(this);
}
@Override
public void onStop() {
super.onStop(); mEventBus.unregister(this);
}的事件消息发布时,可实施 onEvent(…) 或者 onEventMainThread(…) 方法并传入合适的事件类型,让订阅者作出响应。如果使用的是 onEvent(…) 方法,事件消息来自哪个线程,就在哪个线程上处理。(可以实施 onEventMainThread(…) 方法确保事件在主线程上处理,哪怕它碰巧来自后台线程。)

// In some registered component, like a fragment or activity...public void onEventMainThread(NewFriendAddedEvent event){ 
    Friend newFriend = event.getFriend(); // Update the UI or do something in response to event...
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱技术爱生活

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值