首先是要创建一个类用来管理所有活动的启动销毁等,把它命名为AcitivityCollector
代码:
public class ActivityCollector {
public static List<Activity> activities = new ArrayList<>();
public static void addAcitivity(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();
}
}
}
}
然后创建一个所有类的父类BaseActivity
代码:
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityCollector.addAcitivity(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityCollector.removeActivity(this);
}
}
先创建一个登录界面的Activity,布局就用简单的两个TextView和两个EditText加上一个Button
然后是这个Login的后端代码:
public class LoginActivity extends BaseActivity{ private EditText accountEdit; private EditText passwordEdit; private Button login; @Override protected void onCreate(@Nullable 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.button1); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String account = accountEdit.getText().toString(); String password = passwordEdit.getText().toString(); if(account.equals("yozora")&&password.equals("19970914")){ finish();//结束当前活动
//启动MainActivity Intent intent = new Intent(LoginActivity.this,MainActivity.class); startActivity(intent); }else{ Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show(); } } }); } }
之后在MainActivity的布局中加一个Button,这个用来发送广播
MainActivity的代码:
public class MainActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button2); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent("com.example.broadcastbestpractice.FORCE_OFFLINE"); sendBroadcast(intent); } }); } }
现在就需要注册广播了,
修改BaseActivity代码:
public class BaseActivity extends AppCompatActivity { class ForceOfflineReceiver extends BroadcastReceiver{ @Override public void onReceive(final Context context, Intent intent) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("警告"); builder.setMessage("你已经被强制下线,请重新登录"); builder.setCancelable(false); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { ActivityCollector.finishAll();//销毁所有的活动
//回到登录界面(LoginActivity) Intent intent = new Intent(context,LoginActivity.class); context.startActivity(intent); } }); builder.show(); } } private ForceOfflineReceiver receiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityCollector.addAcitivity(this); } @Override protected void onResume() { super.onResume(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("com.example.broadcastbestpractice.FORCE_OFFLINE"); receiver = new ForceOfflineReceiver(); registerReceiver(receiver,intentFilter); } @Override protected void onPause() { super.onPause(); if(receiver!=null){ unregisterReceiver(receiver); receiver = null; } } @Override protected void onDestroy() { super.onDestroy(); ActivityCollector.removeActivity(this); } }
![]()
![]()