zoukankan      html  css  js  c++  java
  • BroadcastReceiver详解

    注册

    静态注册

    • 新建一个Java类,继承于android.content.BroadcastReceiver,并实现他的onReceiver()方法,下面就是一个名为MBroadcast的广播接收者:
    public class MyBroadcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context,intent.getStringExtra("msg"),Toast.LENGTH_SHORT).show();
        }
    }
    
    <receiver android:name=".MyBroadcast">
          <intent-filter>
          <action android:name="android.intent.action.MY_BROADCAST"/>
          <category android:name="android.intent.category.DEFAULT"/>
          </intent-filter>
    </receiver>
    

    动态注册

    public class MainActivity extends Activity {
    
        private MyBroadcast myBroadcast;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            myBroadcast = new MyBroadcast();
            IntentFilter filter = new IntentFilter();
            filter.addAction("android.intent.action.MY_BROADCAST");
            registerReceiver(myBroadcast,filter);
        }
    
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            unregisterReceiver(myBroadcast);
        }
    }
    

    发送广播:

            Intent intent = new Intent("android.intent.action.MY_BROADCAST");
            intent.putExtra("msg","hello receiver");
            sendBroadcast(intent);
    

    当收到广播之后,广播接收者就会执行onReveiver()方法;

    广播的种类

    普通广播(Normal Broadcast)

    • 普通广播对于多个接收者来说,完全是异步的,通常每个对象都无需等待即可接受到广播,接收者直接相互不会有影响,接收者无法终止广播

    有序广播

    • 我们重点说说有序广播,有序广播是按一定的优先级顺序发送的(根据android:priority,最大值是int的最大值),广播一层一层的往下发送,中间可以停在向下发送(abortBroadcast(); ),下面我们创建三个广播接收者:
    public class FirstBroad extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context,intent.getStringExtra("msg")+1,Toast.LENGTH_SHORT).show();
            setResultData("我已经被接受过一次了");
        }
    }
    
    public class SecondBroad extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context,getResultData()+intent.getStringExtra("msg")+2,Toast.LENGTH_SHORT).show();
            setResultData("我已经被接受过二次了");
            //abortBroadcast();
        }
    }
    
    public class ThirdBroad extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context,getResultData()+intent.getStringExtra("msg")+3,Toast.LENGTH_SHORT).show();
        }
    }
    
    • 注册有序广播:
    <receiver android:name=".FirstBroad">
                <intent-filter android:priority="1000">
                    <action android:name="android.coderwei.orderbroadcast"/>
                </intent-filter>
            </receiver>
            <receiver android:name=".SecondBroad">
                <intent-filter android:priority="800">
                    <action android:name="android.coderwei.orderbroadcast"/>
                </intent-filter>
            </receiver>
    
            <receiver android:name=".ThirdBroad">
                <intent-filter android:priority="500">
                    <action android:name="android.coderwei.orderbroadcast"/>
                </intent-filter>
            </receiver>
    
    • 发送有序广播:
    Intent intent = new Intent("android.coderwei.orderbroadcast");
            intent.putExtra("msg","hello receiver");
            sendOrderedBroadcast(intent,null);
    

    其中setResultData可以向下一个广播接收者发送数据。
    sendOrderedBroadcast(intent,null);中的第二个参数为null的时候,不需要接收者声明指定的权限,不为null则需要声明指定的权限,在AndroidManifest.xml中定义一个权限:

    <permission android:name="permission.MY_BROADCAST_PERMISSION" />  
    

    然后使用这个权限:

    <uses-permission android:name="permission.MY_BROADCAST_PERMISSION" />  
    

    然后null改成"permission.MY_BROADCAST_PERMISSION"

    GitHub:https://github.com/godfunc
    博客园:http://www.cnblogs.com/godfunc
    Copyright ©2019 Godfunc
  • 相关阅读:
    laravel5.2总结--blade模板
    laravel5.2总结--响应
    laravel5.2总结--请求
    git总结
    laravel5.2总结--路由
    Get与Post的一些总结
    python库安装
    iptables的recent模块
    iptables
    dmucs与distcc
  • 原文地址:https://www.cnblogs.com/Godfunc/p/6549440.html
Copyright © 2011-2022 走看看