Android BroadcastReceiver

来源:互联网 发布:淘宝屋加盟 编辑:程序博客网 时间:2024/05/16 16:19

1.关于BroadcastReceiver

(1)什么是IntentFilter?
把BroadcastReceiver想象成一个收音机,现在要收听一个电台(89.4Hz,瞎编的);近下来是不是要选台选到89.4Hz啊,IntentFilter就相当于这个选台的,创建IntentFilter时传递的字符串就相当于暗号,谁说出这个暗号,就会被BroadcastReceiver接收到
代码:

        //LOGIN_SUCCESS,LOGOUT_SUCCESS,LOGTASK_FAILURE,LOGTASK_DONE都是字符串        IntentFilter filter = new IntentFilter(LOGIN_SUCCESS);        filter.addAction(LOGOUT_SUCCESS);        filter.addAction(LOGTASK_FAILURE);        filter.addAction(LOGTASK_DONE);        //注册广播,注册时需要指定IntentFilter拦截那些暗号        //BroadcastReceiverHelper是一个自定义的类,继承BroadcastReceiver类        receiver = new BroadcastReceiverHelper();        registerReceiver(receiver,filter);

(2)google对BroadcastReceiver的描述:

Base class for code that will receive intents sent by sendBroadcast().

If you don’t need to send broadcasts across applications, consider using this class with LocalBroadcastManager instead of the more general facilities described below. This will give you a much more efficient implementation (no cross-process communication needed) and allow you to avoid thinking about any security issues related to other applications being able to receive or send your broadcasts.

You can either dynamically register an instance of this class with Context.registerReceiver() or statically publish an implementation through the <receiver> tag in your AndroidManifest.xml.

Note: If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won’t receive intents when paused, and this will cut down on unnecessary system overhead). Do not unregister in Activity.onSaveInstanceState(), because this won’t be called if the user moves back in the history stack.

https://developer.android.com/reference/android/content/BroadcastReceiver.html


大概意思:BroadcastReceiver是用来接收sendBroadcast()发过来的Intent,如果只是在自己应用程序内部使用的话,建议使用LocalBroadcastManager来实现,可以避免其它应用-响应-应用程序内部广播,引发安全问题;可以调用Context.registerReceiver()动态注册广播,也可以在AndroidManifest.xml通过<receiver>节点声明;如果在Activity.onResume() 中注册广播,你应该在Activity.onPause()中取消注册,不要在Activity.onSaveInstanceState()中取消注册,如果用户从回退栈中返回到此Activity,该方法不会调用,不取消注册广播,程序运行会出错,并提示没有取消注册。

2.BroadcastReceiver的使用

说明:为了结构更清晰,下面只展示了部分代码,全部代码请到:https://github.com/huanjinzi/CampusMVP

Activity_A.class

public class Activity_A extends AppCompatActivity {    public static final String LOGTASK_DONE ="com.campus.huanjinzi.campusmvp.LOGTASK_DONE";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_a);    }    @Override    protected void onResume() {        super.onResume();        //注册广播        IntentFilter filter = new IntentFilter(Activity_A.LOGTASK_DONE);        receiver = new BroadcastReceiverHelper();        registerReceiver(receiver,filter);    }    @Override    protected void onPause() {        super.onPause();        /*unregisterReceiver(receiver);*/    }    @Override    protected void onDestroy() {        super.onDestroy();        //取消注册        //因为要接收到Activity_B发过来的广播,所以要在这取消注册广播        unregisterReceiver(receiver);    }}

Activity_B.class

public class Activity_B extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_b);        //启动 Activity_B时就发送了一个 Activity_A.LOGTASK_DONE 的广播        //BroadcastReceiverHelper 的 IntentFilter 能拦截 LOGTASK_DONE 暗号        this.sendBroadcast(new Intent(Activity_A.LOGTASK_DONE));    }}

BroadcastReceiverHelper.class

public class BroadcastReceiverHelper extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        //context就是注册这个广播的 Activity_A        //Activity_A activity = (Activity_A)context; //得到Activity_A对象        Hlog.i("BroadcastReceiverHelper", context.toString() + intent.getAction());    }}

这样可以实现Activity_B通知Activity_A,可以在BroadcastReceiverHelper中操作Activity_A中的控件,感觉结束得有点快,又不知道说什么…

0 0
原创粉丝点击