目录
实现一个强制下线的案例
1.创建ActivityManager
类,统一管理所有Activity的销毁工作
public class ActivityManager {
private static List<Activity> activities = new ArrayList<>();
public static void addActivity(Activity activity){
activities.add(activity);
}
public static void removeActivity(Activity activity){
activities.add(activity);
}
/**
* 销毁所以活动
* */
public static void finishAllActivity(){
for (Activity activity: activities) {
if (!activity.isFinishing()){
activity.finish();
}
}
activities.clear();
}
}
2.创建强制下线广播接收器 ForceOfflineReceiver
public class ForceOfflineReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle("提示");
dialog.setMessage("您的账号被迫下线,请重新登录");
dialog.setCancelable(false);
dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityManager.finishAllActivity(); //销毁所有活动
Intent intent = new Intent(context, LoginActivity.class);
context.startActivity(intent); //重启LoginActivity
}
});
dialog.show();
}
}
3.创建AppConstanct
类,统一管理App用到的常量:
/**
* App用到的常量
* */
public class AppConstanct {
//强制下线广播常量
public final static String BROADCAST_FORCE_OFFLINE = "com.example.broadcastbestpractice.FORCE_OFFLINE";
}
4.创建基类BaseActivity
类,其它Activity继承该Activity
基类BaseActivity
统一管理 广播接收器 的注册和注销:
-
方法
onResume
活动进入活动栈顶端,注册广播接收器
registerReceiver(forceOfflineReceiver, intentFilter);
-
方法
onPause
活动离开活动栈顶端,注销广播接收器
unregisterReceiver(forceOfflineReceiver);
-
Activity的生命周期参见:
Activity的生命周期
public class BaseActivity extends AppCompatActivity {
private ForceOfflineReceiver forceOfflineReceiver;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityManager.addActivity(this);
}
/**
* 前台生存期 。活动在 onResume() 方法和 onPause()方法之间所经历的就是前台生存期。在前台生存期内,
* 活动总是处于运行状态的,此时的活动是可以和用户进行交互的,我们平时看到和接触最多的也就是这个状态下的活动
* */
@Override
protected void onResume() {
super.onResume();
/*--------------------------------------------------
活动进入活动栈顶端,注册广播接收器
* -------------------------------------------------*/
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(AppConstanct.BROADCAST_FORCE_OFFLINE);
forceOfflineReceiver = new ForceOfflineReceiver();
registerReceiver(forceOfflineReceiver, intentFilter);
}
/**
* 前台生存期 。活动在 onResume() 方法和 onPause()方法之间所经历的就是前台生存期。在前台生存期内,
* 活动总是处于运行状态的,此时的活动是可以和用户进行交互的,我们平时看到和接触最多的也就是这个状态下的活动
* */
@Override
protected void onPause() {
super.onPause();
/*--------------------------------------------------
活动离开活动栈顶端,注销广播接收器
* -------------------------------------------------*/
if (forceOfflineReceiver != null){
unregisterReceiver(forceOfflineReceiver);
forceOfflineReceiver = null;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityManager.removeActivity(this);
}
}
5.创建LoginActivity
- LoginActivity作为启动界面;
AndroidMainifest.xml
将LoginActivity设置为启动Activity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastbestpractice">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"></activity>
</application>
</manifest>
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:text="账号"/>
<EditText android:id="@+id/et_account"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:text="密码"/>
<EditText android:id="@+id/et_password"
android:inputType="textPassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<Button android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="登录"/>
</LinearLayout>
LoginActivity
继承BaseActivity
public class LoginActivity extends BaseActivity {
private EditText et_account;
private EditText et_password;
private Button btn_login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
et_account = findViewById(R.id.et_account);
et_password = findViewById(R.id.et_password);
btn_login = findViewById(R.id.btn_login);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String account = et_account.getText().toString();
String password = et_password.getText().toString();
if (account.equals("admin") && password.equals("123456")){
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}else {
Toast.makeText(LoginActivity.this, "账号或密码不正确",Toast.LENGTH_SHORT).show();
}
}
});
}
}
6.修改MainActivity
activity_main.xml
添加一个按钮,点击发送强制下线广播
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_force_offline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="148dp"
android:text="强制下线"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
- MainActivity 继承
BaseActivity
sendBroadcast(intent);
发送强制下线广播
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_force_offline = findViewById(R.id.btn_force_offline);
btn_force_offline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(AppConstanct.BROADCAST_FORCE_OFFLINE);
sendBroadcast(intent); //发送强制下线广播
}
});
}
}
7.效果图
-
登录界面:
-
强制下线: