1、创建ActivityCollector管理所有活动
public class ActivityCollector {
public static List < Activity > activities = new ArrayList < Activity > ( ) ;
public static void addActivity ( Activity activity) {
activities. add ( activity) ;
}
public static void removeActivity ( Activity activity) {
activities. remove ( activity) ;
}
public static void finsihAll ( ) {
for ( Activity activity: activities) {
if ( ! activity. isFinishing ( ) ) {
activity. finish ( ) ;
}
}
}
}
2、创建所有活动的父类
public class BaseActivity extends Activity {
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
ActivityCollector . addActivity ( this ) ;
}
@Override
protected void onDestroy ( ) {
super . onDestroy ( ) ;
ActivityCollector . removeActivity ( this ) ;
}
}
3、登录布局 login.xml
< LinearLayout xmlns: android= "http://schemas.android.com/apk/res/android"
android: layout_width= "match_parent"
android: layout_height= "match_parent"
android: orientation= "vertical"
android: gravity= "center"
>
< EditText
android: id= "@+id/name"
android: layout_width= "200dp"
android: layout_height= "50dp"
android: hint= "输入用户名"
android: textColorHint= "@color/gary"
android: drawableLeft= "@drawable/ic_baseline_face_24"
android: drawablePadding= "10dp"
android: paddingLeft= "10dp"
/ >
< EditText
android: id= "@+id/psw"
android: layout_width= "200dp"
android: layout_height= "50dp"
android: hint= "输入密码"
android: textColorHint= "@color/gary"
android: inputType= "textPassword"
android: drawableLeft= "@drawable/ic_baseline_face_24"
android: drawablePadding= "10dp"
android: paddingLeft= "10dp"
/ >
< Button
android: id= "@+id/btn_login"
android: layout_width= "200dp"
android: layout_height= "50dp"
android: textSize= "20sp"
android: text= "登录" / >
< / LinearLayout >
4、创建LoginActivity继承BaseActivity
public class LoginActivity extends BaseActivity {
private EditText name;
private EditText psw;
private Button btn_login;
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R . layout. login) ;
name = findViewById ( R . id. name) ;
psw = findViewById ( R . id. psw) ;
btn_login = findViewById ( R . id. btn_login) ;
btn_login. setOnClickListener ( new View. OnClickListener ( ) {
@Override
public void onClick ( View view) {
String nameString = name. getText ( ) . toString ( ) . trim ( ) ;
Log . e ( "0723" , "nameString:" + nameString) ;
String pswString = psw. getText ( ) . toString ( ) . trim ( ) ;
if ( nameString. equals ( "Admin" ) && pswString. equals ( "123456" ) ) {
Intent intent = new Intent ( LoginActivity . this , MainActivity . class ) ;
startActivity ( intent) ;
finish ( ) ;
} else {
Toast . makeText ( LoginActivity . this , "账户密码不正确" , Toast . LENGTH_SHORT) . show ( ) ;
}
}
} ) ;
}
}
5、activity_main.xml主界面
< LinearLayout xmlns: android= "http://schemas.android.com/apk/res/android"
android: layout_height= "match_parent"
android: layout_width= "match_parent"
android: orientation= "vertical"
android: gravity= "center"
>
< Button
android: id= "@+id/force_offline"
android: layout_width= "match_parent"
android: layout_height= "100dp"
android: textSize= "30sp"
android: text= "强制下线" / >
< / LinearLayout >
6、MainActivity中显式创建广播,并发送广播
public class MainActivity extends AppCompatActivity {
private ForceOfflineReceiver forceOfflineReceiver;
Button forceOffline;
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R . layout. activity_main) ;
forceOffline = findViewById ( R . id. force_offline) ;
IntentFilter forceFilter = new IntentFilter ( ) ;
forceFilter. addAction ( "com.example.a0723.FORCE_OFFLINE" ) ;
forceOfflineReceiver = new ForceOfflineReceiver ( ) ;
registerReceiver ( forceOfflineReceiver, forceFilter) ;
forceOffline. setOnClickListener ( new View. OnClickListener ( ) {
@Override
public void onClick ( View view) {
Intent intent1 = new Intent ( "com.example.a0723.FORCE_OFFLINE" ) ;
sendBroadcast ( intent1) ;
}
} ) ;
}
@Override
protected void onDestroy ( ) {
super . onDestroy ( ) ;
unregisterReceiver ( forceOfflineReceiver) ;
}
}
7、创建广播接收器ForceOfflineReceiver,onReceive方法中进行弹出对话框,点击ok跳转回LoginActivity中活动
public class ForceOfflineReceiver extends BroadcastReceiver {
@Override
public void onReceive ( Context context, Intent intent) {
AlertDialog. Builder dialogBuilder = new AlertDialog. Builder ( context) ;
dialogBuilder. setTitle ( "Warning" ) ;
dialogBuilder. setMessage ( "你被强制下线了,请重新登录" ) ;
dialogBuilder. setCancelable ( false ) ;
dialogBuilder. setPositiveButton ( "OK" , new DialogInterface. OnClickListener ( ) {
@Override
public void onClick ( DialogInterface dialogInterface, int i) {
ActivityCollector . finsihAll ( ) ;
Intent intent = new Intent ( context, LoginActivity . class ) ;
context. startActivity ( intent) ;
}
} ) ;
AlertDialog alertDialog = dialogBuilder. create ( ) ;
alertDialog. show ( ) ;
}
}
8、配置AndroidManifest.xml文件,换主文件
< activity android: name= ".MainActivity" / >
< activity android: name= ".LoginActivity" >
< intent- filter>
< action android: name= "android.intent.action.MAIN" / >
< category android: name= "android.intent.category.LAUNCHER" / >
< / intent- filter>
< / activity>