zoukankan      html  css  js  c++  java
  • android广播接收器BroadcastReceiver

    首先看一下什么是 BroadcastReceiver

    BroadcastReceiver:直译是“广播接收者”,所以它的作用是用来接收发送过来的广播的。

    那我们有必要知道:什么是广播。广播,我的理解就是系统中消息的一种变种;就是当一个事件发生时,比如,系统突然断网,系统就发一个广播消息给所有的接收者,所有的接收者在得到这个消息之后,就知道,啊哦,现在没网络了,我的程序应该怎么办,比如显示默认图片、提示用户等。前面,我们说了,BroadcastReceiver就是一个广播消息接收者。

    另外我还要提一下,广播之间信息的传递是通过Intent对象来传递的;在《详解Intent》系列文章中,我讲了,Intent调用分为显示调用的隐式调用两种,由于这里能通知到所有的接收者,所以肯定不能利用显示调用,只有利用隐式调用Intent对象了。(这里的隐式调用,并不是真正意义上的Intent隐式调用,因为Intent隐式调用,当出现很多匹配应用时,会以列表形式提示用户选择一个启动,而这里不同的地方在于,当有很多匹配项时,会给所有的匹配项都发一个消息,我说隐式调用,只是方便大家理解构造Intent的方法,即必须利用构造隐式Intent的方法来构造)

    1,创建一个空项目,然后new一个新的BroadcastReceiver(new--->other)MyReceiver.java

     1 public class MyReceiver extends BroadcastReceiver {
     2 
     3     //用于隐式调用与注册
     4     public  static final String ACTION = "examples.ouc.com.broadcastreceiver.intent.action.MyReceiver";
     5     public MyReceiver() {
     6     }
     7 
     8     //监控广播操作是否完成
     9     @Override
    10     public void onReceive(Context context, Intent intent) {
    11 
    12         //通过intent传递信息
    13         System.out.println("Receive news, and the news is :" + intent.getStringExtra("data"));
    14     }
    15 }
    MyReceiver.java

    需要把AndroidManefest中的这句话去掉,否则它是默认一直绑定的

    <receiver
                android:name=".MyReceiver11"
                android:enabled="true"
                android:exported="true"></receiver>

    2,然后在页面中添加几个按钮,用于绑定,解除绑定,和发送

    <Button
            android:id="@+id/btnSendMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="发送消息" />
    
        <Button
            android:id="@+id/btnReg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="注册接收器" />
    
        <Button
            android:id="@+id/btnUnreg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="注销接收器" />
    activity_main

    3,配置broadcast服务

     1 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
     2 
     3     @Override
     4     protected void onCreate(Bundle savedInstanceState) {
     5         super.onCreate(savedInstanceState);
     6         setContentView(R.layout.activity_main);
     7 
     8         findViewById(R.id.btnSendMessage).setOnClickListener(this);
     9         findViewById(R.id.btnReg).setOnClickListener(this);
    10         findViewById(R.id.btnUnreg).setOnClickListener(this);
    11     }
    12 
    13     @Override
    14     public void onClick(View v) {
    15         switch (v.getId()){
    16             case R.id.btnSendMessage:
    17                 //Intent i = new Intent(this,MyReceiver.class);
    18                 //必须采用隐式的
    19                 Intent i = new Intent(MyReceiver.ACTION);
    20                 i.putExtra("data","iCyhuSky");
    21                 sendBroadcast(i);
    22                 break;
    23 
    24             case R.id.btnReg:
    25                 //如果没有绑定,就开启
    26                 if (receiver== null){
    27                     receiver = new MyReceiver();
    28                     registerReceiver(receiver,new IntentFilter(MyReceiver.ACTION));
    29                 }
    30                 break;
    31 
    32             case R.id.btnUnreg:
    33                 //如果开启了,就关闭
    34                 if (receiver!=null){
    35                     unregisterReceiver(receiver);
    36                     receiver = null;
    37                 }
    38                 break;
    39 
    40         }
    41     }
    42 
    43     //标志服务是否绑定
    44     private MyReceiver receiver = null;
    45 }
    MainActivity

    4,然后可发布运行了

    开始时,点击“发送消息”,后台logcat没有输出

    点击“注册接收器”,然后点击“发送消息”,后台logcat会输出我们传递的那句话

    点击“注销接收器”,然后点击“发送消息”,后台logcat就没有输出了!

    5,优先级问题:

    (1)默认状态下:

    如果两个receiver指明到同一个action,那么后创建的优先级比较高,先执行代码

    (2)也可我们人工代码修改优先级

    第一个接收器:

    1 <receiver android:name=".MyReceiver1">
    2             <intent-filter android:priority="9">
    3                 <action android:name="......"
    4             </intent-filter>
    5         </receiver>

    第二个接收器:

    1 <receiver android:name=".MyReceiver2">
    2             <intent-filter android:priority="9">
    3                 <action android:name="......"
    4             </intent-filter>
    5         </receiver>

    从这里我们可以发现,二者除了名字不同外,只有priority有区别,值比较大的优先执行.

    priority汉语就是优先级的意思。。。。

    (3)当我们在优先级比较高的接收器中添加这样一句时(红色),其他的将不再执行:

    1 public void onReceive(Context context, Intent intent) {
    2 
    3         //通过intent传递信息
    4         System.out.println("Receive news, and the news is :" + intent.getStringExtra("data"));
    5         abortBroadcast();
    6     }

    如果只是这样会报错,需要修改MainActivity中的发生发送按钮

     1 public void onClick(View v) {
     2         switch (v.getId()){
     3             case R.id.btnSendMessage:
     4                 //Intent i = new Intent(this,MyReceiver.class);
     5                 //必须采用隐式的
     6                 Intent i = new Intent(MyReceiver.ACTION);
     7                 i.putExtra("data","iCyhuSky");
     8                // sendBroadcast(i);
     9                 sendOrderedBroadcast(i,null);
    10                 break;
  • 相关阅读:
    iOS:Objective-C中Self和Super详解
    调试工具Instruments----Core Animation
    iOS开发之复制字符串到剪贴板
    Copy 和 mutableCopy
    TCP/IP,Http,Socket,XMPP的区别
    iOS程序中的内存分配 栈区堆区全局区(转)
    iOS常见算法(二分法 冒泡 选择 快排)
    老司机带你走进Core Animation
    C# 爬虫小程序
    C# 房贷计算器
  • 原文地址:https://www.cnblogs.com/icyhusky/p/6017082.html
Copyright © 2011-2022 走看看