在前面两节我们发送和接受的都属于系统级别的广播,但是你又没有想过,万一你的应用中的某个广播被别人知道了,人家恶意给你发送各种垃圾广播怎么办?安全性在哪?所以Android为了解决这个问题引入了一套本地广播机制。使用这个机制的广播只能在本应用程序内传播,且接收器也只能接受来自本应用的广播,这样的话刚才说的那个安全问题就已经不算是问题了吧。
下面给一个关于本地广播的一个例子:
public class MainActivity extendsAppCompatActivity {privateIntentFilter intentFilter;privateLocalBroadcastManager localBroadcastManager;privateLocalReceiver localReceiver;
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.sendlocalbradcast);
localBroadcastManager= LocalBroadcastManager.getInstance(this);
btn.setOnClickListener(newView.OnClickListener() {
@Overridepublic voidonClick(View v) {
Intent it= new Intent("wang.zi.yang.fristBroadcast");
localBroadcastManager.sendBroadcast(it);
}
});
intentFilter= newIntentFilter();
intentFilter.addAction("wang.zi.yang.fristBroadcast");
localReceiver= newLocalReceiver();
localBroadcastManager.registerReceiver(localReceiver, intentFilter);
}
@Overrideprotected voidonDestroy() {super.onDestroy();
localBroadcastManager.unregisterReceiver(localReceiver);
}class LocalReceiver extendsBroadcastReceiver {
@Overridepublic voidonReceive(Context context, Intent intent) {
Toast.makeText(context,"接受到广播了", Toast.LENGTH_SHORT).show();
}
}
}
关键的就是使用LocalbroadcastManager的getInstance获取一个实例,然后使用这个实例去操作广播的注册,反注册,发送等。
注意:因为本地广播只在本应用中传播所以注册的时候只可以使用动态注册法。
下面给一个使用广播实现强制下线的DEMO,使用的是广播实现的,在刚才的基础上修改:
public class MainActivity extendsAppCompatActivity {privateIntentFilter intentFilter;privateLocalBroadcastManager localBroadcastManager;privateLocalReceiver localReceiver;
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.sendlocalbradcast);
Button downlinebtn=(Button) findViewById(R.id.senddownlinebradcast);
localBroadcastManager= LocalBroadcastManager.getInstance(this);
btn.setOnClickListener(newView.OnClickListener() {
@Overridepublic voidonClick(View v) {
Intent it= new Intent("wang.zi.yang.fristBroadcast");
localBroadcastManager.sendBroadcast(it);
}
});
downlinebtn.setOnClickListener(newView.OnClickListener() {
@Overridepublic voidonClick(View v) {
Intent it= new Intent("wang.zi.yang.downline");
sendBroadcast(it);
}
});
intentFilter= newIntentFilter();
intentFilter.addAction("wang.zi.yang.fristBroadcast");
localReceiver= newLocalReceiver();
localBroadcastManager.registerReceiver(localReceiver, intentFilter);
}
@Overrideprotected voidonDestroy() {super.onDestroy();
localBroadcastManager.unregisterReceiver(localReceiver);
}class LocalReceiver extendsBroadcastReceiver {
@Overridepublic voidonReceive(Context context, Intent intent) {
Toast.makeText(context,"接受到广播了", Toast.LENGTH_SHORT).show();
}
}
}
ActivityControl是用来记录保存每次打开的Activity:
public classActivityControl {public static List activities = new ArrayList();public static voidaddActivity(Activity activity){if (!activities.contains(activity)){
activities.add(activity);
}
}public static voidremoveActivity(Activity activity){
activities.remove(activity);
}public static voidfinishAll(){for(Activity activity:activities){if (!activity.isFinishing()){
activity.finish();
}
}
}
}
MybaseActivity作为本项目中的所有Activity的父Activity:
public class MybaseActivity extendsActivity {
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
ActivityControl.addActivity(this);
}
@Overrideprotected voidonDestroy() {super.onDestroy();
ActivityControl.removeActivity(this);
}
}
LoginActivity在里面我们实现一个模拟的登录环境:
public class LoginActivity extends MybaseActivity implementsView.OnClickListener {privateEditText accountEdit;privateEditText passwordEdit;privateButton login;
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
accountEdit=(EditText)findViewById(R.id.account);
passwordEdit=(EditText)findViewById(R.id.password);
login=(Button) findViewById(R.id.login);
login.setOnClickListener(this);
}
@Overridepublic voidonClick(View v) {switch(v.getId()){caseR.id.login:
String account=accountEdit.getText().toString();
String password=passwordEdit.getText().toString();if (account.equals("admin") && password.equals("123456")){
Intent it= new Intent(LoginActivity.this,MainActivity.class);
startActivity(it);
finish();
}else{
Toast.makeText(LoginActivity.this,"帐号密码错误",Toast.LENGTH_LONG).show();
}break;
}
}
}
activity_login.xml是登录的界面:
DownLineReceiver用来接受我们发送的强制下线的广播,并在里面做强制下线的操作:
public class DownLineReceiver extendsBroadcastReceiver {
@Overridepublic void onReceive(finalContext context, Intent intent) {
AlertDialog.Builder dialogBuilder= newAlertDialog.Builder(context);
dialogBuilder.setTitle("Warning!");
dialogBuilder.setMessage("You are forced to be offline. Please tyr to login again.");
dialogBuilder.setCancelable(false);
dialogBuilder.setPositiveButton("OK", newDialogInterface.OnClickListener(){
@Overridepublic void onClick(DialogInterface dialog, intwhich) {
ActivityControl.finishAll();
Intent it= new Intent(context, LoginActivity.class);
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(it);
}
});
AlertDialog alertDialog=dialogBuilder.create();
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
}
}
最后在AndroidManifest.xml中的注册如下:
其中的功能很全,代码很简单,只要把这个DEMO理解透彻了,在工作中懂得变通,基本上就可以应对一般的工作内容了。