思路:在界面上弹出一个对话框,让用户无法进行任何操作,必须点击对话框中的确定按钮,然后回到登录界面即可。
import android.app.Activity;
import java.util.ArrayList;
import java.util.List;
//该类用于管理所有活动,因为强制下线功能需要先关闭所有活动
public class ActivityCollector {
public static List<Activity> activities = new ArrayList<>();
//添加活动致活动列表
public static void addActicity(Activity activity){
activities.add(activity);
}
//从活动列表移除活动
public static void removeActicity(Activity activity){
activities.remove(activity);
}
//销毁活动列表中的所有活动
public static void finishAll(){
for (Activity activity:activities){
if (!activity.isFinishing()){
activity.finish();
}
}
}
}
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.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
//该类为所有活动的基类
public class BaseActivity extends AppCompatActivity {
private ForceOfflineReceiver receiver; //广播接收器对象
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityCollector.addActicity(this);
}
//在onResume()和onPause()在注册和取消注册广播接收器
//是为了保证只有处于栈顶的活动才能接收到强制下线广播
//其他的活动不应该也没必要接收该强制下线广播
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("FORCE_OFFLINE");
receiver = new ForceOfflineReceiver();
registerReceiver(receiver,intentFilter);
}
@Override
protected void onPause() {
super.onPause();
if (receiver!=null){
unregisterReceiver(receiver);
receiver = null;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityCollector.removeActicity(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 dialogInterface, int i) {
ActivityCollector.finishAll();//销毁所有活动
Intent intent = new Intent(context,LoginActivity.class);
context.startActivity(intent); //重新启动登陆活动
}
});
builder.show();
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.thinkpad.broadcastbestpractice.LoginActivity">
<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/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/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:inputType="textPassword"/>
LinearLayout>
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="登录"/>
LinearLayout>
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends BaseActivity {
private EditText accountEdit; //输入账号的EditText对象
private EditText passwordEdit; //输入密码的EditText对象
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 view) {
String account = accountEdit.getText().toString(); //获取账号输入框中的内容
String password = passwordEdit.getText().toString(); //获取密码输入框中的内容
//判断账号密码是否正确;此处的账号:Nicholas 密码: hzf
if (account.equals("Nicholas")&&password.equals("hzf")){
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
finish();
}else{
Toast.makeText(LoginActivity.this,"账号密码输入错误或者为空!",Toast.LENGTH_SHORT).show();
}
}
});
}
}
<LinearLayout 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"
tools:context="com.example.thinkpad.broadcastbestpractice.MainActivity">
<Button
android:id="@+id/force_offline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击发送强制下线广播"/>
LinearLayout>
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends BaseActivity {
private Button forceOffline; //获得强制下线按钮对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
forceOffline = (Button)findViewById(R.id.force_offline); //按钮对象实例化
//为该按钮设置点击事件
forceOffline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//设置广播值并发送
Intent intent = new Intent("FORCE_OFFLINE");
sendBroadcast(intent);
}
});
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thinkpad.broadcastbestpractice">
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<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=".MainActivity">
activity>
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
application>
manifest>
整理学习自郭霖大佬的《第一行代码》
目前小白一名,持续学习Android中,如有错误请批评指正!