Android-动态注册广播和注销广播
Android-动态注册广播和注销广播
静态注册广播
广播机制在安卓开发很常见也很重要,它既可以静态注册,也可以
动态注册和注销,广播可以设置优先级。
首先是静态
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/btnSend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送广播" />LinearLayout>
设置一个按钮用于发送广播
创建一个广播类AgainMyReceiver.java继承BroadcastReceiver
AgainMyReceiver.java
package com.xieth.as.againbroadcastreceiver;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class AgainMyReceiver extends BroadcastReceiver { public AgainMyReceiver() { } @Override public void onReceive(Context context, Intent intent) { Log.d("AgainMyReceiver", "静态广播接收到了消息"); }}
打印日志便于测试查看,然后在AndroidManifest.xml修改
<receiver android:name=".AgainMyReceiver" android:enabled="true" android:exported="true" > receiver>
然后在MainActivity.java定义事件
MainActivity.java
package com.xieth.as.againbroadcastreceiver;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Intent it = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btnSend).setOnClickListener(this); it= new Intent(MainActivity.this, AgainMyReceiver.class); } @Override public void onClick(View v) { int id = v.getId(); switch (id) { case R.id.btnSend: sendBroadcast(it); break; default: break; } }}
运行输出:
点击发送广播按钮
成功接收到广播消息
动态发送广播和注销广播
在这之前把下面这一段删除:
<receiver android:name=".AgainMyReceiver" android:enabled="true" android:exported="true" > receiver>
然后在布局文件添加两个按钮用于注册和注销广播:
activity_main.xml
"http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
接着在广播类添加此字段:
public static final String ACTION = "com.xieth.as.againbroadcastreceiver.action.intent.AgainMyReceiver"
MainActivity.java
package com.xieth.as.againbroadcastreceiver;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;public class MainActivity extends AppCompatActivity implements View.OnClickListener { private AgainMyReceiver myReceiver = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btnSend).setOnClickListener(this); findViewById(R.id.btnReg).setOnClickListener(this); findViewById(R.id.btnUnreg).setOnClickListener(this); } @Override public void onClick(View v) { int id = v.getId(); switch (id) { case R.id.btnSend: Intent i = new Intent(AgainMyReceiver.ACTION); sendBroadcast(i); break; case R.id.btnReg: //注册广播 if (myReceiver == null) { myReceiver = new AgainMyReceiver(); registerReceiver(myReceiver, registerReceiver(myReceiver, new IntentFilter(myReceiver.ACTION));); } break; case R.id.btnUnreg: //注销广播 if (myReceiver != null) { unregisterReceiver(myReceiver); myReceiver = null; } break; default: break; } }}
运行效果:
刚开始点击发送广播,没有输出信息,所以我们在此注册广播,然后再点击发送广播:
广播的优先级
再次新建一个广播类,命名为AgainMyReceiver1.java
package com.xieth.as.againbroadcastreceiver;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class AgainMyReceiver1 extends BroadcastReceiver { public AgainMyReceiver1() { } @Override public void onReceive(Context context, Intent intent) { Log.d("AgainMyReceiver", "AgainMyReceiver1接收到了消息"); }}
AgainMyReceiver.java
package com.xieth.as.againbroadcastreceiver;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class AgainMyReceiver extends BroadcastReceiver { public static final String ACTION = "com.xieth.as.againbroadcastreceiver.action.intent.AgainMyReceiver"; public AgainMyReceiver() { } @Override public void onReceive(Context context, Intent intent) { Log.d("AgainMyReceiver", "AgainMyReceiver接收到了消息"); }}
然后修改AndroidManifest.xml文件
<receiver android:name=".AgainMyReceiver1" android:enabled="true" android:exported="true" > <intent-filter> <action android:name="com.xieth.as.againbroadcastreceiver.action.intent.AgainMyReceiver">action> intent-filter> receiver> <receiver android:name=".AgainMyReceiver" android:enabled="true" android:exported="true" > <intent-filter> <action android:name="com.xieth.as.againbroadcastreceiver.action.intent.AgainMyReceiver">action> intent-filter> receiver>
先不设置优先级,观察输出结果:
由于action是一样的,所以同时接收到了消息,现在设置优先级,修改AndroidManifest.xml文件
<receiver android:name=".AgainMyReceiver1" android:enabled="true" android:exported="true" > <intent-filter android:priority="1"> <action android:name="com.xieth.as.againbroadcastreceiver.action.intent.AgainMyReceiver">action> intent-filter> receiver> <receiver android:name=".AgainMyReceiver" android:enabled="true" android:exported="true" > <intent-filter android:priority="2"> <action android:name="com.xieth.as.againbroadcastreceiver.action.intent.AgainMyReceiver">action> intent-filter> receiver>
然后运行输出:
可以看到AgainMyReceiver优先接收到消息,因为优先级为2,比AgainMyReceiver1的优先级高。
中断广播的发送
我们可以中断广播的发送:
但是发送的方式不一样:是sendOrderedBroadcast()方式发送
修改代码:
case R.id.btnSend: Intent i = new Intent(AgainMyReceiver.ACTION); sendOrderedBroadcast(i, null);// sendBroadcast(i); break;
然后在AgianMyReceiver加入如下代码终止比它优先级低发送广播
使用abortBroadcast()方法
其他不做修改!
@Override public void onReceive(Context context, Intent intent) { Log.d("AgainMyReceiver", "AgainMyReceiver接收到了消息"); abortBroadcast(); }
运行输出:
可以看见成功终止了向AgainMyReceiver1发送广播!
2016年1月13日12:22:45 记录
更多相关文章
- Android(安卓)网络提交数据(使用Asynchronous Http Client)
- 《老罗Android》监听电量变化、Broadcast实现开机启动
- Android应用程序开机开机启动
- [Android实例] Android(安卓)实例SOCKET发送HTTP请求
- [Android(安卓)Pro] 有关Broadcast作为内部类时注册的一些问题
- android BroadcastReceiver相关
- Android自动滚动 轮播循环的ViewPager
- android预览word(WPS预览)
- Android(安卓)向服务器发送get请求乱码问题