android BroadcastReceiver广播接收者是android四大组件之一,通常用于监听系统的变化情况,比如网络连接和电池电量变化,或者用户离线上线之后及时更改用户在线离线状态等等。
BroadcastReceiver广播接收者原理:当用户或系统需要发送某个intent信息时,它必须给接收信息者一个action标识,然后系统filter监听到这个action的标识之后会根据name="xxx"的用户来处理相关的广播信息。
android BroadcastReceiver分为静态注册和动态注册,在开发中动态注册会比较常见一点,静态注册适合于发送广播信号给所有的用户,例如弹出一个新闻广告,不管你在app的哪个页面你都会看到这个广告,而Broadcast Receiver动态注册就比较适合于针对某个Activity页面发送广播信号,比如有人给我发消息,app右下角马上会出现一个红色的标识,提醒我有多少条消息没有阅读等等。
下面来分别使用静态和动态的方式注册广播接收者,如下。
公共部分代码:在activity_main.xml界面中添加一个发送broadcast消息的button按钮,代码如下。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.baidu.broadcastdemo.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送广播消息" android:onClick="sendBroadcast"/> </RelativeLayout>
案例一:静态注册Broadcast Receiver。
1:MainActivity.java中实现sendBroadcast点击事件,用于发送一个广播信息。
package com.baidu.broadcastdemo; import android.content.Context; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //发送broadcast广播的事件 public void sendBroadcast(View view){ Intent intent = new Intent(); //intent给消息接收者携带消息 intent.putExtra("mykey","我是发送过来的消息"); intent.setAction("com.baidu.MY_BROADCAST"); sendBroadcast(intent); } }
2:在AndroidManifest.xml文件中的<application></application>之间加入broadcast广播接收者的监听配置,并指定名为MyReceiver的类来接收发送的广播消息,配置如下。
<receiver android:name=".MyReceiver"> <intent-filter> <action android:name="com.baidu.MY_BROADCAST"></action> </intent-filter> </receiver>
3:MyReceiver类必须要继承BroadcastReceiver类,实现MyReceiver类中的onReceive()方法,并弹出所接收到的消息,代码如下。
public class MyReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { String msg = intent.getStringExtra("mykey"); Toast.makeText(context,"静态收到:"+msg,Toast.LENGTH_SHORT).show(); } }
4:静态注册运行后的效果如图所示,注意提示消息的区别。
案例二:动态注册Broadcast Receiver,不需要在AndroidManifest.xml文件中配置filter过滤器,也不需要指定处理的类是谁,只需要在需要接收消息界面的onCreate()方法中动态注册broadcast receiver就可以了,本案例是在当前页自己发送自己接收,开发中自己根据实际情况。
package com.baidu.broadcastdemo; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { //自定义的动态广播接收者 private DynamicReceiver dynamicReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //创建过滤器 IntentFilter filter = new IntentFilter(); //添加监听的name标识 filter.addAction("com.baidu.MY_BROADCAST"); dynamicReceiver = new DynamicReceiver(); //注册广播接收者 registerReceiver(dynamicReceiver,filter); } //发送broadcast广播的事件 public void sendBroadcast(View view){ Intent intent = new Intent(); //intent给消息接收者携带消息 intent.putExtra("mykey","我是发送过来的消息"); intent.setAction("com.baidu.MY_BROADCAST"); sendBroadcast(intent); } class DynamicReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { String msg = intent.getStringExtra("mykey"); Toast.makeText(context,"动态收到:"+msg,Toast.LENGTH_SHORT).show(); } } }
运行之后如图所示。