以下内容纯粹为本人学习笔记【记录】之用,所听课程(Q群群友百度网盘提供)为极客学院一位老师所讲(老师大名我尚未知晓),如有侵权请告知。在此特别感谢这位老师录制的视频资料。
1、使用BroadcastReceiver
若有其他程序发送了消息到这个接收器,它即可接收到。实质是一种通信方式。
2、动态注册和注销BroadcastReceiver
有时并不希望Broadcast一直处于监听状态,即要求动态注册和注销掉它。
删掉或注释掉AndroidManifest文件中的receiver标签,此时BroadcastReceiver将接收不到消息。
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true"></receiver>
如何注册Receiver?
1)activity_main添加两个按钮来“控制”注册和注销;
2)MainActivity添加按钮对应的点击事件;
3)防止重复多个注册,在点击事件代码外边添加声明receiver代码,用来判断,用来在点击事件里判断。
private MyReceiver receiver = null;
3、BroadcastReceiver的优先级
首先,在AndroidManifest写上receiver标签
<receiver
android:name=".MyReceiver">
<intent-filter>
<action android:name="com.keen.learnbroadcastreceiver.intent.action.MyReceiver" />
</intent-filter>
</receiver>
再创建一个接收器AnotherReceiver,以便观察优先级
后注册的优先级要高,先接收到消息。如何指定优先级?intent-filter有个参数priority,数值高优先级高。若数值相同,那么写在前面的优先级更高。
打印日志结果是:
05-20 15:45:25.484 10256-10256/? D/Receiver_TAG: --------AnotherReceiver接收到了消息------
05-20 15:45:25.486 10256-10256/? D/Receiver_TAG: -------来自CC----MyReceiver
<receiver android:name=".MyReceiver">
<intent-filter android:priority="1">
<action android:name="com.keen.learnbroadcastreceiver.intent.action.MyReceiver" />
</intent-filter>
</receiver>
<receiver android:name=".AnotherReceiver">
<intent-filter android:priority="2">
<action android:name="com.keen.learnbroadcastreceiver.intent.action.MyReceiver" />
</intent-filter>
</receiver>
如何让优先级高的Receiver接收消息,而低于此优先级的Receiver不接收消息?
得先把发送广播的方法改为:sendOrderedBroadcast(i, null);
后将该接收广播的Receiver的onRecerver加上:abortBroadcast();//中断了广播的继续传递
这样就中断了广播继续往优先级更低的接收器里发送了。
名词解释:
deactivation n.非活动化、钝化、惰性化。例:Save file on frame deactivation 自动保存文件
MainActivity.java
package com.keen.learnbroadcastreceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnSendMsg).setOnClickListener(this);
findViewById(R.id.btnReg).setOnClickListener(this);
findViewById(R.id.btnUnReg).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSendMsg:
// Intent i = new Intent(this, MyReceiver.class);
Intent i = new Intent(MyReceiver.ACTION);//隐式
i.putExtra("data", "-------来自CC----MyReceiver");//附加数据
// sendBroadcast(i);//发送一个消息
sendOrderedBroadcast(i, null);//发送顺序广播;第二个参数(权限)为null
break;
case R.id.btnReg:
if (receiver==null) {
receiver = new MyReceiver();//创建一个receiver
registerReceiver(receiver, new IntentFilter("MyReceiver.ACTION"));//注册
}
break;
case R.id.btnUnReg:
if (receiver!=null) {
unregisterReceiver(receiver);//注销
receiver = null;//赋值为null
}
break;
}
}
private MyReceiver receiver = null;
}
AnroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.keen.learnbroadcastreceiver">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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=".MyReceiver">
<intent-filter android:priority="3">
<action android:name="com.keen.learnbroadcastreceiver.intent.action.MyReceiver" />
</intent-filter>
</receiver>
<receiver android:name=".AnotherReceiver">
<intent-filter android:priority="2">
<action android:name="com.keen.learnbroadcastreceiver.intent.action.MyReceiver" />
</intent-filter>
</receiver>
</application>
</manifest>
MyReceiver.java
package com.keen.learnbroadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
public static final String ACTION = "com.keen.learnbroadcastreceiver.intent.action.MyReceiver";
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Receiver_TAG",intent.getStringExtra("data"));
// System.out.println("---------MyReceiver接收到了消息----------");
abortBroadcast();//中断了广播的继续传递
}
}
AnotherReceiver.java
package com.keen.learnbroadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class AnotherReceiver extends BroadcastReceiver {
public AnotherReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Receiver_TAG","--------AnotherReceiver接收到了消息------");
// System.out.println("--------AnotherReceiver接收到了消息------");
}
}
activity_main.xml
<?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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.keen.learnbroadcastreceiver.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView" />
<Button
android:text="发送消息"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:id="@+id/btnSendMsg" />
<Button
android:text="注册接收器Receiver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnSendMsg"
android:id="@+id/btnReg" />
<Button
android:text="注销接收器Receiver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnReg"
android:id="@+id/btnUnReg" />
</RelativeLayout>