强制下线
一:示例图
用户登录后发送一条广播,使其退出
二.管理Activity的类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 finishAll(){
for(Activity act:activities){
if( !act.isFinishing() ){
act.finish();
}
}
//System.exit(0);
}
}
三.Activity基类BaseActivity:
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);
}
}
四.登录界面布局,activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1"
>
<TableRow>
<TextView android:layout_height="wrap_content"
android:text="用户登录"
android:layout_span="2"
android:gravity="center"
android:padding="15dp"
android:layout_margin="15dp"/>
</TableRow>
<TableRow>
<TextView android:layout_height="wrap_content"
android:text="用户名"/>
<EditText android:id="@+id/username"
android:layout_height="wrap_content"
android:hint="请输入账号"
android:textColor="#333"/>
</TableRow>
<TableRow>
<TextView android:layout_height="wrap_content"
android:text="密码"/>
<EditText android:id="@+id/password"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword"
android:textColor="#333"/>
</TableRow>
<TableRow>
<Button android:id="@+id/btn"
android:text="登录"
android:layout_height="wrap_content"
android:layout_span="2"/>
</TableRow>
</TableLayout>
五.登录界面类LoginActivity
public class LoginActivity extends BaseActivity {
EditText username,password;
Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
login = (Button)findViewById(R.id.btn);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(username.getText().toString().equals("admin")
&& password.getText().toString().equals("123456")){
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
}else{
Toast.makeText(LoginActivity.this, "登录错误", Toast.LENGTH_LONG).show();
}
}
});
}
}
六.广播类MyBroadcaseReceiver
public class MyBroadcaseReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
dialogBuilder.setTitle("警告");
dialogBuilder.setMessage("你已经下线了,请重新登录");
dialogBuilder.setCancelable(false);
dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCollector.finishAll(); // 销毁活动
Intent intent = new Intent(context, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
AlertDialog alertDialog = dialogBuilder.create();
// 需要设置AlertDialog的类型,保证在广播接收器中可以正常弹出
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
}
}
七.MainActivity布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/send"
android:text="强制下线"/>
</LinearLayout>
八.MainActivity
public class MainActivity extends BaseActivity {
Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send = (Button)findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.guo.forceoffline.FORCE_OFFLINE");
sendBroadcast(intent);
}
});
}
}
九.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.guo.forceoffline">
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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>
<receiver android:name=".MyBroadcaseReceiver">
<intent-filter>
<action android:name="com.example.guo.forceoffline.FORCE_OFFLINE" />
</intent-filter>
</receiver>
</application>
</manifest>