广播接收者 BroadcastReceiver 示例-2
BaseActivity
/**所有Activity的基类*/public class BaseActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);ActivityCollector.addActivity(this);}@Overrideprotected void onDestroy() {super.onDestroy();ActivityCollector.removeActivity(this);}}
MainActivity
public class MainActivity extends BaseActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Log.i("MainActivity", "++++++onCreate");TextView textView = new TextView(this);textView.setGravity(Gravity.CENTER);textView.setText("请登录");textView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {startActivity(new Intent(MainActivity.this, SecondActivity.class));}});setContentView(textView);}@Overrideprotected void onDestroy() {super.onDestroy();Log.i("MainActivity", "++++++onDestroy");}@Overridepublic void finish() {super.finish();Log.i("MainActivity", "++++++finish");}}
SecondActivity
public class SecondActivity extends BaseActivity {private MyLocalReceiver localReceiver;//本地广播的管理器,该机制发出的广播只会在APP内部传播,且广播接收者也只能收到本APP发出的广播!本地广播无法通过静态方式注册,相比起系统全局广播更加高效private LocalBroadcastManager localBroadcastManager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);TextView textView = new TextView(this);textView.setText("发送本地广播");textView.setGravity(Gravity.CENTER);setContentView(textView);localReceiver = new MyLocalReceiver();IntentFilter intentFilter = new IntentFilter(MyActions.MY_BROADCAST_ACTION_LOGIN_OTHER);//可以通过addAction()方法添加多个ActionlocalBroadcastManager = LocalBroadcastManager.getInstance(this);localBroadcastManager.registerReceiver(localReceiver, intentFilter);//注册广播接收者,设置过滤器textView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {localBroadcastManager.sendBroadcast(new Intent(MyActions.MY_BROADCAST_ACTION_LOGIN_OTHER));}});}@Overrideprotected void onDestroy() {super.onDestroy();localBroadcastManager.unregisterReceiver(localReceiver);Log.i("SecondActivity", "++++++onDestroy");}@Overridepublic void finish() {super.finish();Log.i("SecondActivity", "++++++finish");}}
Activity管理类
/** 一个用于在应用退出时关闭所有Activity的容器管理类 */public class ActivityCollector {private static List<Activity> activities = new ArrayList<Activity>();public static void addActivity(Activity activity) {activities.add(activity);}public static void removeActivity(Activity activity) {activities.remove(activity);}public static void finishAll() {for (Activity activity : activities) {if (!activity.isFinishing()) {activity.finish();}}}}
自定义的Action
public class MyActions {public static final String MY_BROADCAST_ACTION_UNORDERED = "com.bqt.broadcast.songwennuan_unordered";public static final String MY_BROADCAST_ACTION_ORDERED = "com.bqt.broadcast.songwennuan_ordered";public static final String MY_BROADCAST_ACTION_LOGIN_OTHER = "com.bqt.broadcast.local_login_other";}
一级广播接收者
public class Level1Receiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {if (intent.getAction().equals(MyActions.MY_BROADCAST_ACTION_ORDERED)) {//如果是无序广播,执行setResultData、abortBroadcast会报错!System.out.println("Level1消息:" + getResultData());setResultData("给农民兄弟发5000块钱");//更改接收到的广播内容,发给低优先级的应用abortBroadcast();//阻止广播,但是不能阻止FinalReceiver接收广播,但能更改广播内容之后再发给他。放在setResultData前后都可以。} else if (intent.getAction().equals(MyActions.MY_BROADCAST_ACTION_UNORDERED)) {System.out.println("Level1消息:" + intent.getStringExtra("msg"));}}}
二级广播接收者
public class Level2Receiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {if (intent.getAction().equals(MyActions.MY_BROADCAST_ACTION_ORDERED)) {System.out.println("Level2消息:" + getResultData());setResultData("给农民兄弟发2000块钱");//更改接收到的广播内容,发给低优先级的应用} else if (intent.getAction().equals(MyActions.MY_BROADCAST_ACTION_UNORDERED)) {System.out.println("Level2消息:" + intent.getStringExtra("msg"));}}}
最终广播接收者
public class FinalReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {if (intent.getAction().equals(MyActions.MY_BROADCAST_ACTION_ORDERED)) {System.out.println("Final消息:" + getResultData());} else if (intent.getAction().equals(MyActions.MY_BROADCAST_ACTION_UNORDERED)) {System.out.println("Final消息:" + intent.getStringExtra("msg"));}}}
本地广播
/** 接收到本地广播后弹出对话框,关闭所有Activity,并调到MainActivity */public class MyLocalReceiver extends BroadcastReceiver {@Overridepublic void onReceive(final Context context, Intent intent) {AlertDialog alertDialog = new AlertDialog.Builder(context).setTitle("警告:").setMessage("您的账号在别处登录,请重新登陆~").setCancelable(false).setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {ActivityCollector.finishAll();Intent intent = new Intent(context, MainActivity.class);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//广播中启动Activity需要设置此flag,因为Activity需要一个栈context.startActivity(intent);}}).create();alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); //系统对话框权限:SYSTEM_ALERT_WINDOWalertDialog.show();}}
清单文件
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.bqt.broadcast"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="17"android:targetSdkVersion="21" /><!-- 系统对话框权限 --><uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name=".MainActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".SecondActivity" /><!-- —————————————————————自定义广播————————————————————— --><!-- 本地广播 --><receiver android:name=".myreceiver.MyLocalReceiver" ><intent-filter><action android:name="com.bqt.broadcast.local_login_other" /></intent-filter></receiver><!-- 无序广播和有序广播 --><receiver android:name=".myreceiver.Level1Receiver" ><intent-filter android:priority="1000" ><action android:name="com.bqt.broadcast.songwennuan_unordered" /><action android:name="com.bqt.broadcast.songwennuan_ordered" /></intent-filter></receiver><receiver android:name=".myreceiver.Level2Receiver" ><intent-filter android:priority="500" ><action android:name="com.bqt.broadcast.songwennuan_unordered" /><action android:name="com.bqt.broadcast.songwennuan_ordered" /></intent-filter></receiver><receiver android:name=".myreceiver.FinalReceiver" ><intent-filter android:priority="10" ><action android:name="com.bqt.broadcast.songwennuan_unordered" /><action android:name="com.bqt.broadcast.songwennuan_ordered" /></intent-filter></receiver></application></manifest>
广播接收者 BroadcastReceiver 示例-2的更多相关文章
- 广播接收者 BroadcastReceiver 示例-1
广播机制概述 Android广播分为两个方面:广播发送者和广播接收者,通常情况下,BroadcastReceiver指的就是广播接收者.广播作为Android组件间的通信方式,可以使用的场景如下: 1 ...
- Android系统编程入门系列之广播接收者BroadcastReceiver实现进程间通信
在前边几篇关于Android系统两个重要组件的介绍中,界面Activity负责应用程序与用户的交互,服务Service负责应用程序内部线程间的交互或两个应用程序进程之间的数据交互.看上去这两大组件就能 ...
- Android - 广播接收者 - BroadcastReceiver
BroadcastReceiver 介绍: 广播是一种广泛运用的在应用程序之间传输信息的机制 .而 BroadcastReceiver 是对发送出来的广播 进行过滤接收并响应的一类组件 接受一种或者多 ...
- Android中广播接收者BroadcastReceiver详解
1. 接收系统的广播步骤 (1) 新建一个类继承BroadcastReceiver 以监听sd卡状态的广播接收者为例 public class SdCardBroadcastReceiver ext ...
- Android学习笔记_19_广播接收者 BroadcastReceiver及其应用_窃听短信_拦截外拨电话
一.广播接收者类型: 广播被分为两种不同的类型:“普通广播(Normal broadcasts)”和“有序广播(Ordered broadcasts)”. 普通广播是完全异步的,可以在同一时刻(逻辑上 ...
- 广播接收者BroadcastReceiver
BroadcastReceiver与activity,service有完整的生命周期不同,BroadcastReceiver本质上是一系统级别的监听器,专门负责监听各程序发出的broadcast.与程 ...
- 广播接收者 BroadcastReceiver
1. 分为动态注册和静态注册, 静态注册在清单文件里配置即可.动态创建为代码手动添加. 在锁屏广播中, 使用静态创建消息接受不成功, 原因未知. 动态即可. 代码如下: 2. 创建类, 继承与Broa ...
- 广播发送者&广播接收者介绍
1.广播接收者 广播接收者简单地说就是接收广播意图的Java类,此Java类继承BroadcastReceiver类,重写: public void onReceive(Context context ...
- Android(java)学习笔记175:BroadcastReceiver之 外拨电话的广播接收者
首先我们示例工程一览表如下: 1.首先我们还是买一个收音机,定义一个OutCallReceiver继承自BroadcastReceiver,onReceive()方法中定义了监听到广播,要执行的操作: ...
随机推荐
- explicit 关键字
C++ explicit关键字用来修饰类的构造函数,表明该构造函数是显式的,既然有"显式"那么必然就有"隐式",那么什么是显示而什么又是隐式的呢? 如果c++类 ...
- 详解CSS选择器、优先级与匹配原理
原文链接:http://polaris1119.javaeye.com/blog/764428 作为一个Web开发者,掌握必要的前台技术也是很重要的,特别是在遇到一些实际问题的时候.这里给大家列举一个 ...
- Bash shell 笔记总结(一) 转自http://www.bubuko.com/infodetail-509992.html,谢谢原作者
本文是上课笔记总结,涉及细节知识点会在以后文章说明! bash脚本编程: 脚本程序:解释器解释执行: shell: 交互式接口:编程环境: shell: 能够提供一些内部命令,并且能通过PATH环境变 ...
- IE的有条件注释详解
IE的有条件注释是一种专有的(因此是非标准的).对常规(X)HTML注释的Miscrosoft扩展.顾名思义,有条件注释使你能够根据条件(比如浏览器版本)显示代码块.尽管是非标准的,但是有条件注释对于 ...
- 《python基础教程》笔记之 基础知识
数字相关 在Python程序前加上 from __future__ import division 或者在解释器里面直接执行它,或者通过命令行运行Python时使用命令开关-Qnew,会使单斜线不再整 ...
- iOS——文件操作NSFileManager (创建、删除,复制,粘贴)
iOS——文件操作NSFileManager (创建.删除,复制,粘贴) iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视 ...
- ulimit小结
1. limits是一个进程的资源,会被子进程继承 2. soft limit -S, hard limits -H hard limits只能被root用户修改,启动的时候会加载配置/etc/s ...
- Drag & Drop and File Reader
参考 : http://www.html5rocks.com/zh/tutorials/file/dndfiles/ http://blog.csdn.net/rnzuozuo/article/det ...
- Thread 1 cannot allocate new log的问题分析
http://blog.csdn.net/zonelan/article/details/7613519 http://leoguan.blog.51cto.com/816378/584494 htt ...
- C51系列RAM寄存器表
特殊功能寄存器地址表 SFR 符号 字节 地址 位地址和位名称 D7 D6 D5 D4 D3 D2 D1 D0 P0口 P0 80H P0.7 P0.6 P0.5 P0.4 P0.3 P0.2 P0. ...