Android 广播接收器

来源:互联网 发布:强力数据恢复软件官网 编辑:程序博客网 时间:2024/05/17 00:51

1

broadcast receiver 广播,接收器 Android四大组件之一;
Android权威指南这本书的结构实在是,四大组件离得这么远,早忘记概念了。。

broadcast intent 我们看一下自定义广播的发送

 public static final  String ACTION_SHOW_NOTIFICATION="com.example.asus.photogallery.SHOW_NOTIFICATION";  sendBroadcast(new Intent(ACTION_SHOW_NOTIFICATION));

我们创建了一个inten 然后通过sendBroadcast 发送出去;那么谁来接收呢?

我们对比一下intent的发送和接收:

              Intent intent = new           //发送者 Intent("com.example.activitytest.ACTION_START");                intent.addCategory("com.example.activitytest.MY_CATEGORY");                startActivity(intent);

我们把intent 看成是要嫁人的二八少女,少女没有明确目标,只有想法说男子该如何如何。。

<activity android:name="com.example.activitytest.SecondActivity">  //接收者        <intent-filter>            <action android:name="com.example.activitytest.ACTION_START"/>              <category android:name="android.intent.category.DEFAULT"/>            <category android:name="com.example.activitytest.MY_CATEGORY"/>        </intent-filter>    </activity>

xml文件中定义SecondActivity具有的特性,action,category;

intent看了一圈,选中谁,就和谁走了,和别人没关系,当然也可能只有一个,或者一个也没有;
手机上我们打开一个网址,如果没有默认应用的话,就会给一大堆待选程序,我们选择后,就直接打开网址了;

我们再来看看 broadcast receiver
下面这个例子是用来响应手机启动完成的接收者;手机启动时系统会发送广播,我们接收广播,并且log一条语句;

public class StartupReceiver extends BroadcastReceiver {//接收者    @Override    public void onReceive(Context context, Intent intent) {        Log.i(Tag,"received broadcast intent:" +intent.getAction());    }}
 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>//获取权限 <receiver android:name=".StartupReceiver">            <intent-filter>                <action android:name="android.intent.action.BOOT_COMPLETED"></action>//声明该接收者可以响应这个广播;            </intent-filter>        </receiver>

如此一看,broadcast intent 和普通intent也没有什么区别嘛;都写了自己想要的特性,接收者也都需要填写自己的特性,然后两者凑一块,完成。。。

但是呢,我们手机只要开机就会发送开机完成的广播,你能接收,我为什么不能呢?

没错广播,之所以成为广播,是因为这货是个皇帝,可以n个老婆;

2

那有n个老婆就会存在一些问题喽
问题一:动态receiver 皇宫里有好多娘娘,难免有今天不想接客的。。。。

public class VisibleFragment extends Fragment {    private BroadcastReceiver mOnShowNotification = new BroadcastReceiver() {        @Override        public void onReceive(Context context, Intent intent) {        //接收皇上,会发生些什么呢?        }    };    @Override    public  void  onResume(){        super.onResume();        IntentFilter filter = new IntentFilter(PollService.ACTION_SHOW_NOTIFICATION);        getActivity().registerReceiver(mOnShowNotification,filter,PollService.PREM_PRIVATE,null);       //在太监那里登记说,今天接客;    }    @Override    public  void  onPause()    {        super.onPause();        getActivity().unregisterReceiver(mOnShowNotification);        //木有登记。。    }}

问题二:权限!谁家的皇帝啊?总不能别人家的跑来; 今天临幸的是什么等级?是妃还是嫔?

 <permission android:name="com.example.asus.photogallery.PRIVATE" android:protectionLevel="signature"></permission>   //定制了一个权限;    <uses-permission android:name="com.example.asus.photogallery.PRIVATE"></uses-permission> //使用该权限;
 public static  final String PREM_PRIVATE="com.example.asus.photogallery.PRIVATE";  sendBroadcast(new Intent(ACTION_SHOW_NOTIFICATION),PREM_PRIVATE); //皇帝说今天要临幸的是妃子;
   getActivity().registerReceiver(mOnShowNotification,filter,PollService.PREM_PRIVATE,null);  //登记时,添加权限;我是妃子

如果限制皇帝是谁家的呢?

 <receiver android:name=".NotificationReceiver" android:exported="false">//别家的皇帝不行。。。

问题三:有序broadcast intent

简单地说sendBroadcast() ,广播发出后,接收者是同时做出响应的,sendOrderedBroadcast()则是广播发出后,一条一条的执行;并且在之前执行的接收者可以对广播进行处理甚至终止广播的传递;传递的顺序在xml

  <intent-filter android:priority="-999"> //数字为优先级。

有序广播的创建:

 void  showBackgroundNotification(int requestCode,Notification notification)    {        Intent intent=new Intent(ACTION_SHOW_NOTIFICATION);        intent.putExtra("REQUEST_CODE",requestCode);        intent.putExtra("NOTIFICATION",notification);        sendOrderedBroadcast(intent,PREM_PRIVATE,null,null, Activity.RESULT_OK,null,null);        //第一个intent为result receiver 结果接收器,最后一个运行的接收器;    }

result receiver 的实现

public class NotificationReceiver extends BroadcastReceiver {    private static final  String TAG ="notification";    @Override    public void onReceive(Context context, Intent intent) {        Log.i(TAG,"received result :"+getResultCode());        if(getResultCode()!= Activity.RESULT_OK)            return;        int requestCode = intent.getIntExtra("REQUEST_CODE", 0);        Notification notification = (Notification)intent.getParcelableExtra("NOTIFICATION");        NotificationManager notificationManager =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);        notificationManager.notify(requestCode,notification);    }}

当然别忘记在AndroidManifest.xml声明;

  <receiver android:name=".NotificationReceiver" android:exported="false">            <intent-filter android:priority="-999">                <action android:name="com.example.asus.photogallery.SHOW_NOTIFICATION"></action>            </intent-filter>        </receiver>

当然还有实现:

 showBackgroundNotification(0,notification);
1 0
原创粉丝点击