美文网首页
广播学习

广播学习

作者: Crane_FeiE | 来源:发表于2018-09-11 07:59 被阅读0次

    广播的类型

    • 标准广播(Normal Broadcasts)--- 异步执行的广播,所有接收器几乎同时收到,效率较高但无法截断
    • 有序广播(Ordered Broadcasts)--- 同步执行的广播,接收器一个个地接到广播,一个接收器的逻辑执行完后才会传到下一个接收器,过程可截断

    注册广播接收器

    1.动态注册----在代码中注册接收器,仅当应用启动时可以收到广播

    Sample Code

    
    public class ReceiverTestActivity extends AppCompatActivity {
        private NetworkChangeReceiver mReceiver;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_receiver_test);
            initReceiver();
        }
    
        private void initReceiver() {
            IntentFilter filter = new IntentFilter();
            filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
            mReceiver = new NetworkChangeReceiver();
            registerReceiver(mReceiver, filter);
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            if(mReceiver != null){
                unregisterReceiver(mReceiver);
            }
        }
    
        class NetworkChangeReceiver extends BroadcastReceiver {
    
            @Override
            public void onReceive(Context context, Intent intent) {
                //TODO
            }
        }
    
        public static Intent formatIntent(Context originActivity){
            return new Intent(originActivity, ReceiverTestActivity.class);
        }
    }
    

    2.静态注册---- 在Manifest中注册,可以在不运行的时候也接到广播,并处理相应逻辑

        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
    
            <!--exported = "true": Broadcasts outside this app can deliver to this receiver-->
            <receiver android:name=".BootReceiver"
                android:enabled="true"
                android:exported="true"> 
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED"/>
                </intent-filter>
            </receiver>
        </application>
    

    发送自定义广播

    Sample Code

    1. 发送标准广播

        //you should register a custom broadcast receiver in order to handle this broadcast
        private void emmitCustomBroadcast(){
            // String param is your custom intent action
            Intent intent = new Intent("com.example.crane.myfirstline.action.custom_broadcast");
            sendBroadcast(intent);
        }
    

    2.发送有序广播

        //you should register a custom broadcast receiver in order to handle this broadcast
        private void emmitOrderedCustomBroadcast(){
            // String param is your custom intent action, 
            Intent intent = new Intent("com.example.crane.myfirstline.action.custom_broadcast");
            
            //String param is your permission set for this broadcast, it's nullable
            sendOrderedBroadcast(intent, null);
        }
    

    3.阻断有序广播

    在广播接收器中onReceive()方法中逻辑末尾添加abortBroadcast()方法

    4.使用本地广播

    本地广播可以保证该广播只在应用内被接收,防止外部利用广播侵入应用;
    使用LocalBroadcastManager的实例中的sendBroadcast(Intent intent)方法
    Sample Code

        private void emmitLocalBroadcast() {
            //get Instance of LocalBroadcastManager
            LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
            Intent intent = new Intent("com.example.crane.myfirstline.action.local_broadcast");
            localBroadcastManager.sendBroadcast(intent);
        }
    

    作业

    利用广播实现强制下线功能

    相关文章

      网友评论

          本文标题:广播学习

          本文链接:https://www.haomeiwen.com/subject/zglygftx.html