Broadcast

2018-06-11  本文已影响0人  LazyerRookie

一、广播是什么

在Android中,它是一种广泛运用在应用程序之间传输信息的机制,Android中我们发送广播内容是一个Intent,这个Intent中可以携带我们要发送的数据。

二、广播的使用场景

三、广播的种类

四、广播接收器

广播接收器是专门用来接收广播信息的,分为静态注册和动态注册两种:

public class BootCompleteReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"广播接收器",Toast.LENGTH_SHORT).show();
    }
}

静态注册的广播接收器一定要在AndroidManifest.xml文件中注册后才可以使用,注册代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.strive.broadcast">
    <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">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name=".BootCompleteReceiver"
            android:enabled="true"
            android:exported="true">
           <intent-filter>
               <action android:name="android.intent.action.BOOT_COMPLETED"/>
           </intent-filter>
        </receiver>
    </application>
</manifest>
public class MainActivity extends AppCompatActivity {
    private IntentFilter intentFilter;
    private NetWorkChangeRecriver netWorkChangeRecriver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        netWorkChangeRecriver = new NetWorkChangeRecriver();
       //注册广播接收器
        registerReceiver(netWorkChangeRecriver,intentFilter);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消注册广播接收器
        unregisterReceiver(netWorkChangeRecriver);
    }
    class NetWorkChangeRecriver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            //获取系统服务类,专门用于管理网络连接
            ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            if(!("".equals(networkInfo))&&networkInfo.isAvailable()){
                Toast.makeText(context, "网络连接正常", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(context, "网络连接失败", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

五、广播内部实现机制

六、本地广播

为了解决广播安全性的问题,Android引入了一套本地广播机制,使用这个机制发出的广播只能够在应用程序内部进行传递,并且广播接收器也只能接受应用程序内部的广播。
本地广播的发送和注册广播接收器都需要使用LocalBroadcastManager来对广播进行管理,并提供发送广播和注册广播接收器的方法。

public class MainActivity extends AppCompatActivity {
   private IntentFilter intentFilter;
   private LocalBroadcastManager localBroadcastManager;
   private LocalReceiver localReceier;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        localBroadcastManager = LocalBroadcastManager.getInstance(this);

        intentFilter = new IntentFilter();
        intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
        localReceier = new LocalReceiver();
        localBroadcastManager.registerReceiver(localReceier,intentFilter);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        localBroadcastManager.unregisterReceiver(localReceier);
    }
    class LocalReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "localbroadcast", Toast.LENGTH_SHORT).show();
        }
    }
}

本地广播的特点

本地广播的内部实现机制

上一篇下一篇

猜你喜欢

热点阅读