Train of thought:
Enter the account password to log in, enter the main page, click the forced logoff button on the main page, and issue the forced logoff broadcast in the button event. Implement the broadcast receiver in BaseActivity, destroy all activities in the broadcast receiver, and then restart the login interface.
However, the forced logoff function needs to be implemented in every activity interface, so we need to implement the registration and logout of broadcast receivers in every activity. In this way, no matter when the system sends the forced offline broadcast, we can receive the broadcast and realize the forced offline function. The best way to achieve this is to register and unregister the broadcast in the onResume() and onPause() methods of BaseActivity.
Source code address of the first line of code (Guo Lin)
BaseActivity code is as follows
public class BaseActivity extends AppCompatActivity {
private ForceOfflineReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityCollector.addActivity(this);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.broadcastbestpractice.FORCE_OFFLINE");
receiver = new ForceOfflineReceiver();
registerReceiver(receiver, intentFilter);//Register broadcast receiver
}
@Override
protected void onPause() {
super.onPause();
if (receiver != null) {
unregisterReceiver(receiver);//Unregister broadcast receiver
receiver = null;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityCollector.removeActivity(this);
}
class ForceOfflineReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);//Build a dialog
builder.setTitle("Warning");
builder.setMessage("You are forced to be offline. Please try to login again.");
builder.setCancelable(false);//Dialog set to non cancelable
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {//Set OK button for dialog box
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCollector.finishAll(); // Destroy all activities
Intent intent = new Intent(context, LoginActivity.class);
context.startActivity(intent); // Restart LoginActivity
}
});
builder.show();
}
}
}
LoginActivity code is as follows
public class LoginActivity extends BaseActivity {
private EditText accountEdit;
private EditText passwordEdit;
private Button login;
@Override
protected void onCreate(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(new View.OnClickListener() {
@Override
public void onClick(View v) {
String account=accountEdit.getText().toString();
String password=passwordEdit.getText().toString();
//If the account is admin and 123456, the login is successful
if (account.equals("admin") && password.equals("123456")) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(LoginActivity.this,"account or password is invalid",Toast.LENGTH_SHORT).show();
}
}
});
}
}
MainActivity code is as follows
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button forceOffline = (Button) findViewById(R.id.force_offline);
forceOffline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent =new Intent("com.example.broadcastbestpractice.FORCE_OFFLINE");
sendBroadcast(intent);
}
});
}
}