Android-动态注册广播和注销广播
来源:互联网 发布:windows环境开发ios 编辑:程序博客网 时间:2024/04/27 20:44
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
<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="发送广播" /> <Button android:id="@+id/btnReg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册广播" /> <Button android:id="@+id/btnUnreg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注销广播" /></LinearLayout>
接着在广播类添加此字段:
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-动态注册广播和注销广播
- android监听与注册广播注销广播
- 【Android 一般进阶】动态广播注册注销时机
- 【Android 一般进阶】动态广播注册注销时机
- Android静态注册广播和动态注册广播的区别
- Android广播之注册广播(包括静态广播和动态广播的注册)源码分析
- android 动态注册广播
- android动态注册广播
- Android动态注册广播
- myapplication991静态广播注册和动态广播
- 【Android】动态注册广播接收器
- 【Android】动态注册广播接收器
- 【Android】动态注册广播接收器
- android动态注册广播接收器
- Android动态注册广播接收器
- Android动态注册监听广播
- Android 动态注册广播接收者
- android动态注册广播实例
- 手游《白话三国》挂机称霸 一统天下(设计思路整理)
- Sublime
- 如何排查问题
- JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510)异常
- C#源代码—委托的演示,利用多路广播机制优化
- Android-动态注册广播和注销广播
- 命令行下运行 Matlab 及 函数
- Your PHP installation appears to be missing the MySQL extension.。。。。。。
- mysql数据库锁机制
- 怎样快速学习一个系统
- 分享一个链接
- C#源代码—使用哈希表保存学生信息
- 菜鸟在线与你一起分析:程序员如何让自己变得不可替代?
- 【杭电oj】1994 - 利息计算(水)