ActivityCollector用于管理所有的活动
baseActivity是所有活动继承的基类
MainActivity是实现登陆判断功能的
SecondActivity是实现发送广播实现强制下线功能的
ActivityCollector类
package com.example.forced_offline;import android.app.Activity;import java.util.ArrayList;import java.util.List;public class ActivityCollector { public static List activityList = new ArrayList<>();//存储所有活动的数列 public static void addActivity(Activity activity) { activityList.add(activity); }//向数列中添加活动的方法 public static void removeActivity(Activity activity) { activityList.remove(activity); }//从数列中移除活动的方法 //删除数列中所有活动的方法 public static void finishAllActivity() { for (Activity activity : activityList) { if (!activity.isFinishing()) { activity.finish(); } } activityList.clear(); }}
baseActivity类
package com.example.forced_offline;import android.os.Bundle;import android.util.Log;import androidx.annotation.Nullable;import androidx.appcompat.app.AppCompatActivity;public class baseActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); //当活动被创建时向活动管理类中添加 ActivityCollector.addActivity(this); Log.d("baseActivity",getClass().getSimpleName()); } @Override protected void onDestroy() { super.onDestroy(); //当活动被销毁时将其从活动管理类中移除 ActivityCollector.removeActivity(this); }}
MainActivity活动
package com.example.forced_offline;import androidx.appcompat.app.alertDialog;import androidx.appcompat.app.AppCompatActivity;import android.content.DialogInterface;import android.content.Intent;import android.os.Bundle;import android.text.TextWatcher;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends baseActivity implements View.onClickListener { //输入框和按钮 EditText id, password; Button login;//存储正确账号密码的常量 private static final String CORRECT_ID = "admin"; private static final String CORRECT_PASSWORD = "123456"; //存储被输入的账号密码 String idText, passwordText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //控件绑定 initUI(); //按钮点击事件 login.setonClickListener(this); }//控件初始化函数 public void initUI() { id = findViewById(R.id.id); password = findViewById(R.id.password); login = findViewById(R.id.login); }//按钮点击函数 @Override public void onClick(View view) { switch (view.getId()) { case R.id.login: //获取输入的账号密码 idText = id.getText().toString(); passwordText = password.getText().toString(); //判断输入的账号密码与正确的是否一致 if ((idText.equals(CORRECT_ID)) && (passwordText.equals(CORRECT_PASSWORD))) { //如果一致跳转到下一个活动 Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivity(intent); } else { //如果不一致,跳出一个对话框,使其退出活动 alertDialog.Builder dialog = new alertDialog.Builder(MainActivity.this); dialog.setTitle("密码错误"); dialog.setMessage("请退出重新登陆!!!"); dialog.setCancelable(false); dialog.setPositiveButton("OK", new DialogInterface.onClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { ActivityCollector.finishAllActivity(); } }); dialog.show(); } break; } }}
SecondActivity活动
package com.example.forced_offline;import androidx.appcompat.app.AppCompatActivity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.View;import android.widget.Button;import java.util.zip.Inflater;public class SecondActivity extends baseActivity { //按钮 Button offLine; //自定义的接收广播 MyBroadcastReceiver broadcastReceiver; //自定义action final String ACTION_OF_MY_OFFLINE = "ACTION_OF_MY_OFFLINE"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); //初始化控件 initUI(); //初始化广播 broadcastReceiver = new MyBroadcastReceiver(); //初始化和添加广播的intentFilter,添加需要监听的action IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(ACTION_OF_MY_OFFLINE); registerReceiver(broadcastReceiver, intentFilter); offLine.setonClickListener(new View.onClickListener() { @Override public void onClick(View view) { //发送自定义广播即通过intent发送自定义action Intent intent = new Intent(ACTION_OF_MY_OFFLINE); sendBroadcast(intent); } }); } //接收广播类 class MyBroadcastReceiver extends BroadcastReceiver { //接收到响应广播所做出的动作 @Override public void onReceive(Context context, Intent intent) { switch (intent.getAction()) { case ACTION_OF_MY_OFFLINE: //关闭所有活动 ActivityCollector.finishAllActivity(); break; } } } //初始化控件 private void initUI() { offLine = findViewById(R.id.offLine); } //释放广播资源 @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(broadcastReceiver); }}
mainactivity的布局代码
<?xml version="1.0" encoding="utf-8"?>
secondactivity的布局代码
<?xml version="1.0" encoding="utf-8"?>