Android开发(4):广播接收者BroadcastReceiver

个人笔记整理

广播接收者BroadcastReceiver

广播机制简介

  • 标准广播 (Normal broadcasts)是一种完全异步执行的广播,在广播发出之后,所有的广播接收器几乎都会在同一时刻接收到这条广播消息,因此它们之间没有任何先后顺序可言。这种广播的效率会比较高,但同时也意味着它是无法被截断的。
    
  • 有序广播 (Ordered broadcasts)则是一种同步执行的广播,在广播发出之后,同一时刻只会有一个广播接收器能够收到这条广播消息,当这个广播接收器中的逻辑执行完毕后,广播才会继续传递。所以此时的广播接收器是有先后顺序的,优先级高的广播接收器就可以先收到广播消息,并且前面的广播接收器还可以截断正在传递的广播,这样后面的广播接收器就无法收到广播消息了。
    

接收系统广播

动态注册监听网络变化

新建一个类,让它继承自BroadcastReceiver ,并重写父类的onReceive() 方法。当有广播到来时,onReceive() 方法就会得到执行

public class MainActivity extends AppCompatActivity {
    private IntentFilter intentFilter;
    private NetworkChangeReceiver networkChangeReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 创建一个intent过滤器
        intentFilter = new IntentFilter();
        // 添加想要接收的intent请求
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        // 实例化 Receiver类
        networkChangeReceiver = new NetworkChangeReceiver();
        // 动态注册广播接收者
        registerReceiver(networkChangeReceiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 取消注册广播接收者
        unregisterReceiver(networkChangeReceiver);
    }
    class NetworkChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // 当收到广播时,弹窗提示网络状态改变
            Toast.makeText(context, "network changes", Toast.LENGTH_SHORT).show();
        }
    }
}

或者当网络状态改变时,提示当前有网络还是无网络

public class MainActivity extends AppCompatActivity {
    ...
    class NetworkChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // 得到ConnectivityManager实例(一个系统服务类,专门用于管理网络连接)
            ConnectivityManager connectionManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
            // 得到NetworkInfo的实例
            NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
            // 通过调用NetworkInfo的isAvailable()方法,来判断出当前有无网络
            if (networkInfo != null && networkInfo.isAvailable()) {
                Toast.makeText(context, "network is available",
                    Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "network is unavailable",
                    Toast.LENGTH_SHORT).show();
            }
        }
    }

}

同时,访问网络状态需要在AndroidManifest.xml中声明权限

静态注册实现开机启动提醒

需要在AndroidManifest.xml中注册receiver

<receiver
    android:name=".BootCompleteReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <!-- 开机事件广播 -->
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

同时需要添加相应监听开机的权限

之后便可以添加一个Receiver.java,并在onReceive()中编写相应控制逻辑

public class BootCompleteReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Boot Complete", Toast.LENGTH_LONG).show();
    }
}

不要在onReceive() 方法中添加过多的逻辑或者进行任何的耗时操作,因为在广播接收器中是不允许开启线程的,当onReceive() 方法运行了较长时间而没有结束时,程序就会报错。

发送自定义广播

发送标准广播
定义一个自己的广播接收器
public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_
            SHORT).show();
    }
}
<receiver
    android:name=".MyBroadcastReceiver"
    android:enabled="true"
    android:exported="true">

    <intent-filter>
        <action android:name="com.example.broadcasttest.MY_BROADCAST"/>
    </intent-filter>
</receiver>

接收一个值为com.example.broadcasttest.MY_BROADCAST的广播

public void onClick(View v) {
    Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
    sendBroadcast(intent);
}

// 同
public void onClick(View v) {
    Intent intent = new Intent();
    intent.setAction("com.example.broadcasttest.MY_BROADCAST");
    intent.setPackage("com.example.broadcasttest");
    sendBroadcast(intent);
}

由于广播是使用Intent进行传递的,因此你还可以在Intent中携带一些数据传递给广播接收器。

发送有序广播

发送时

public void onClick(View v) {
    Intent intent = new
        Intent("com.example.broadcasttest.MY_BROADCAST");
    sendOrderedBroadcast(intent, null);
}

接收时,通过android.priority属性来设置优先级

<receiver
    android:name=".MyBroadcastReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter android:priority="100">
        <action android:name="com.example.broadcasttest.MY_BROADCAST" />
    </intent-filter>
</receiver>

同时通过abortBroadcast()方法来截断广播(之后的广播接收器无法收到)

public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "received in MyBroadcastReceiver",
        Toast.LENGTH_SHORT).show();
    abortBroadcast();
}

使用本地广播

public class MainActivity extends AppCompatActivity {
    private IntentFilter intentFilter;
    private LocalReceiver localReceiver;
    private LocalBroadcastManager localBroadcastManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        localBroadcastManager = LocalBroadcastManager.getInstance(this); // 获取实例
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
                localBroadcastManager.sendBroadcast(intent); // 发送本地广播
            }
        });
        intentFilter = new IntentFilter();
        intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
        localReceiver = new LocalReceiver();
        localBroadcastManager.registerReceiver(localReceiver, intentFilter); // 注
        册本地广播监听器
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        localBroadcastManager.unregisterReceiver(localReceiver);
    }
    class LocalReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "received local broadcast", Toast.LENGTH_SHORT).

               show();
        }
    }
}

与动态注册广播接收器一样,不过现在是通过LocalBroadcastManagergetInstance() 方法得到了它的一个实例,然后在注册广播接收器的时候调用的是LocalBroadcastManagerregisterReceiver() 方法,在发送广播的时候调用的是LocalBroadcastManagersendBroadcast() 方法

本地广播是无法通过静态注册的方式来接收

使用广播来实现强制下线功能

@TODO

上一篇:Android 摄像头预览悬浮窗,可拖动,可显示在其他app上方


下一篇:1.2