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>