1、效果图:
2、文件:
3、ActivityCollector.java
package com.example.jcs.mybroadcastreceiverdemo;
import android.app.Activity;
import java.util.ArrayList;
import java.util.List;
//定义一个类来管理所有活动
public class ActivityCollector {
public static List list = new ArrayList();
public static void addActivity(Activity activity) {
list.add(activity);
}
public static void removeActivity(Activity activity) {
list.remove(activity);
}
public static void finishAll() {
for (Activity a : list) {
//遍历集合是否到最后一个
if (!a.isFinishing()) {
a.finish();
}
}
}
}
4、Base.java
package com.example.jcs.mybroadcastreceiverdemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
public class Base extends AppCompatActivity {
private Receiver receiver;
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.jcs.mybroadcastreceiverdemo.FORCE_OFFLINE");
receiver = new Receiver();
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);
}
//增加一个广播接收器
class Receiver 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("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//移除所有的页面
ActivityCollector.finishAll();
//在转到login
Intent intent = new Intent(context,Login.class);
context.startActivity(intent);
}
});
//PS:一定要show();
dialog.show();
}
}
}
5、Login.java
package com.example.jcs.mybroadcastreceiverdemo;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/** * Created by JCS on 2017/6/27. */
public class Login extends Base {
private EditText user_name;
private EditText password;
private Button btn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
user_name = (EditText) findViewById(R.id.user);
password = (EditText) findViewById(R.id.password);
btn = (Button) findViewById(R.id.login_btn);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String name = user_name.getText().toString().trim();
String pass = password.getText().toString().trim();
//判断账号密码
if (name.equals("admin") && pass.equals("123")) {
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(Login.this, "error", Toast.LENGTH_SHORT).show();
}
}
});
}
}
6、MainActivity.java
package com.example.jcs.mybroadcastreceiverdemo;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Base {
private Button btn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.send_broadcast);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//点击就发送广播
Intent intent = new Intent("com.example.jcs.mybroadcastreceiverdemo.FORCE_OFFLINE");
sendBroadcast(intent);
}
});
}
}
7、activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.jcs.mybroadcastreceiverdemo.MainActivity">
<Button android:id="@+id/send_broadcast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送广播" />
RelativeLayout>
8、login_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账号:" />
<EditText android:id="@+id/user" android:layout_width="match_parent" android:layout_height="wrap_content" />
LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账号:" />
<EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" />
LinearLayout>
<Button android:id="@+id/login_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录" />
LinearLayout>
9、清单文件:(把login为启动页面)
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".MainActivity">activity>
<activity android:name=".Login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
application>