点击发送强制下线按钮,弹出确定框,点击确定跳到登录界面。
创建用于操作活动的类ActivityCollector:
public class ActivityCollector {
public static List<Activity> activities = new ArrayList<>();
public static void addActivity(Activity activity) {
activities.add(activity);
}
public static void removeActivity(Activity activity) {
activities.remove(activity);
}
public static void finishAll() {
for (Activity activity : activities) {
if (!activity.isFinishing()) {
activity.finish();
}
}
}
}
创建基础活动类BaseActivity继承AppCompatActivity,这样不用在每个活动中实现弹出警告框,接收广播:
public class BaseActivity extends AppCompatActivity {
private ForceOfflineReceiver forceOfflineReceiver;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityCollector.addActivity(this);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("top.xiexiaodong.broadcast.FORCE_OFFLINE");
forceOfflineReceiver = new ForceOfflineReceiver();
registerReceiver(forceOfflineReceiver, intentFilter);
}
@Override
protected void onPause() {
super.onPause();
if (forceOfflineReceiver != null) {
unregisterReceiver(forceOfflineReceiver);
forceOfflineReceiver = 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); // 创建一个警告对话框
builder.setTitle("警告");
builder.setMessage("你被强制下线,请重新登录");
builder.setCancelable(false);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCollector.finishAll(); // 销毁所有活动
Intent intent = new Intent(context, LoginActivity.class);
context.startActivity(intent); // 重新启动LoginActivity
}
});
builder.show();
}
}
}
之后所有活动类继承BaseActivity类。
创建登录活动和布局,主要是输入账号和密码进行判断登录,默认账号admin,密码123456:
<?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:textSize="18sp"
android:text="账号:"
android:layout_gravity="center_vertical"
android:layout_width="90dp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/account"
android:layout_gravity="center_vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:textSize="18sp"
android:text="密码:"
android:layout_gravity="center_vertical"
android:layout_width="90dp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/password"
android:inputType="textPassword"
android:layout_gravity="center_vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/login"
android:text="登录"
android:layout_width="match_parent"
android:layout_height="60dp" />
</LinearLayout>
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();
// 如果密码123456,账号admin,则认为登录成功
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();
}
}
});
}
}
最后,在MainActivity中,添加一个按钮,并实现它的点击事件,发送一条强制下线广播:
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("top.xiexiaodong.broadcast.FORCE_OFFLINE");
sendBroadcast(intent);
}
});
}
}
作者:a191030148 发表于2017/2/18 23:41:31 原文链接
阅读:33 评论:0 查看评论