Android通过广播实现强制下线功能
我们首先定义一个activity集合类,在这个类里面主要添加三个方法,分别是
1.添加activity
2.删除activity
3.关闭所有activity
如下图:
package cn.xiayiye.forceoff_line;import android.app.Activity;import java.util.ArrayList;import java.util.List;/*** 创 建 者:下一页5(轻飞扬)* 创建时间:/2/28.11:05* 个人小站:http://wap.yhsh.ai(已挂)* 最新小站:http://www.iyhsh.icoc.in* 联系作者:企鹅 13343401268* 博客地址:/xiayiye5* 空间名称:XiaYiYeMap* 项目包名:cn.xiayiye.forceoff_line*/class ActivityCollector {private static List<Activity> activities = new ArrayList<>();//存放activity的集合//添加activitystatic void addActivity(Activity acy) {activities.add(acy);}//移除activitystatic void removeActivity(Activity acy) {activities.remove(acy);}//销毁所有activitystatic void finishAll() {for (Activity activity : activities) {if (!activity.isFinishing()) {activity.finish();//关闭所有activity}}}}
2.我们在写一个baseactivity,让所有的activity继承activity,然后在baseactivity里面调用添加activity和关闭所有activity的方法即可:
package cn.xiayiye.forceoff_line;import android.app.Activity;import android.app.AlertDialog;import android.content.BroadcastReceiver;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.util.Log;/*** 创 建 者:下一页5(轻飞扬)* 创建时间:/2/28.11:10* 个人小站:http://wap.yhsh.ai(已挂)* 最新小站:http://www.iyhsh.icoc.in* 联系作者:企鹅 13343401268* 博客地址:/xiayiye5* 空间名称:XiaYiYeMap* 项目包名:cn.xiayiye.forceoff_line*/public class BaseActivity extends Activity {private Off_LineReceiver off_lineReceiver;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);if (savedInstanceState != null) {String save = savedInstanceState.getString("save");Log.e("打印保存后取出的信息", save);}//添加所有活动页面ActivityCollector.addActivity(this);}@Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);outState.putString("save", "要保存的信息");}@Overrideprotected void onDestroy() {super.onDestroy();ActivityCollector.removeActivity(this);//移除所有活动页面}@Overrideprotected void onResume() {super.onResume();IntentFilter intentFilter = new IntentFilter("com.yhsh.off-line");off_lineReceiver = new Off_LineReceiver();registerReceiver(off_lineReceiver, intentFilter);//注册广播}@Overrideprotected void onPause() {super.onPause();if (off_lineReceiver != null) {unregisterReceiver(off_lineReceiver);//解绑广播off_lineReceiver = null;}}class Off_LineReceiver extends BroadcastReceiver {@Overridepublic void onReceive(final Context context, Intent intent) {Log.e("打印动作:", intent.getAction());if ("com.yhsh.off-line".equals(intent.getAction())) {//弹框强制下线AlertDialog.Builder alert = new AlertDialog.Builder(context);alert.setCancelable(false);//点击对话框以外不取消对话框alert.setTitle("警告").setMessage("你已被强制下线").setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {//关闭所有的activity,直接进入登录页面ActivityCollector.finishAll();startActivity(new Intent(context, LoginActivity.class));//通过下面的一个属性会让页面跳转之间无任何动画效果// overridePendingTransition(0, 0);}}).show();//显示对话框}}}}
然后我们先写一个简单的登录页面:
当账号密码正确方可进入主页面
package cn.xiayiye.forceoff_line;import android.content.Intent;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class LoginActivity extends BaseActivity implements View.OnClickListener {private EditText et_act;private EditText et_pwd;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_login);et_act = (EditText) findViewById(R.id.et_act);et_pwd = (EditText) findViewById(R.id.et_pwd);Button bt_login = (Button) findViewById(R.id.bt_login);bt_login.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.bt_login:login();break;}}private void login() {String trim_act = et_act.getText().toString().trim();String trim_pwd = et_pwd.getText().toString().trim();if (!TextUtils.isEmpty(trim_act) && !TextUtils.isEmpty(trim_pwd)) {if (trim_act.equals("admin") && trim_pwd.equals("123456")) {//登录startActivity(new Intent(LoginActivity.this, MainActivity.class));finish();//关闭登录页面} else {Toast.makeText(this, "账号密码不正确!", Toast.LENGTH_SHORT).show();}} else {Toast.makeText(this, "账号密码不能为空!", Toast.LENGTH_SHORT).show();}}}
3.然后再主页面自定义一个广播,发送给所有activity,当然所有页面都是继承baseactivity的,所以我们只需要在baseactivity里面接受自定义的广播即可实现关闭所有页面个添加所有activity:
package cn.xiayiye.forceoff_line;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends BaseActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button off_line = (Button) findViewById(R.id.off_line);off_line.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//发送强制下线广播Intent intent = new Intent("com.yhsh.off-line");sendBroadcast(intent);}});}}
通过我们在baseactivity里面接收到自定义的广播来关闭所有activity页面,重新进入到登录页面,这样一来我们就实现了任意页面强制下线功能了。
动态图如下:
如果看以上代码还是不懂得请点击源码:
源码下载