Android 强制下线 Android运用BroadcastReceiver实现强制下线
programmerteny 人气:0思路:首先进入登录界面,输入账号和密码后登陆到主界面,在主界面通过点击按钮发送一条强制下线的广播,广播接收者收到广播后重新进入登陆界面。
新建项目Broadcast03。强制下线功能需要先关闭所有的活动,然后返回到登录界面。所以先创建一个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 activity : activities) { if (!activity.isFinishing()) { activity.finish(); } } } }
在这个类中定义了三个方法,分别用来添加活动、移除活动与关闭所有活动。然后新建一个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); } }
在BaseActivity类中,调用了ActivityCollector类的addActivity()和removeActivity()方法,这样其他类继承BaseActivity类的时候就会自动重写这两个方法。
编写登录界面的布局login.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:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="10dp" android:gravity="center" android:text="account:" android:textSize="16dp" /> <EditText android:id="@+id/et_account" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_weight="1" android:hint="please input account" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="10dp" android:gravity="center" android:text="password:" android:textSize="16dp" /> <EditText android:id="@+id/et_password" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_weight="1" android:hint="please input password" android:inputType="numberPassword" /> </LinearLayout> <Button android:id="@+id/bt_login" android:layout_width="120dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Login" /> </LinearLayout>
编写登录活动。新建LoginActivity继承自BaseActivity,实现登录的逻辑,这里设置当帐号为admin,密码为123456时才能登陆成功。
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.login); accountEdit = (EditText) findViewById(R.id.et_account); passwordEdit = (EditText) findViewById(R.id.et_password); login = (Button) findViewById(R.id.bt_login); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String account = accountEdit.getText().toString(); String password = passwordEdit.getText().toString(); if ("admin".equals(account) && "123456".equals(password)) { Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); finish(); } else { Toast.makeText(LoginActivity.this, "输入的帐号或者密码有误,请重新输入", Toast.LENGTH_SHORT).show(); } } }); } }
登录成功会进入到MainActivity主界面,我们在activity_main布局中加入一个Button,通过点击这个按钮发送一条强制下线的广播。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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.beijing.broadcast03.MainActivity"> <Button android:id="@+id/bt_force_offline" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send force offline broadcast" /> </RelativeLayout>
MainActivity界面点击按钮发送广播的逻辑:
public class MainActivity extends AppCompatActivity { private Button forceOffline; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); forceOffline = (Button) findViewById(R.id.bt_force_offline); forceOffline.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("com.beijing.broadcast03.FORCE_OFFLINE"); sendBroadcast(intent); } }); } }
这里发送了一条值为com.beijing.broadcast03.FORCE_OFFLINE的广播。创建广播接收者ForceOfflineReceiver继承自BroadcastReceiver,ForceOfflineReceiver收到广播后就会弹出一个对话框来达成强制下线的目的:
public class ForceOfflineReceiver extends BroadcastReceiver { @Override public void onReceive(final 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 dialog, int which) { ActivityCollector.finishAll();//销毁所有Activity Intent intent = new Intent(context, LoginActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent);//重新启动LoginActivity } }); AlertDialog alertDialog = dialogBuilder.create(); alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);//需要设置AlertDialog的类型,以便能够在广播接收者中正常弹出 alertDialog.show(); } }
在此通过AlertDialog.Builder来构建一个对话框,注意一定要调用setCancelable()方法将对话框设置为不可取消(按返回键是无法退出对话框的)。接着使用setPositiveButton()方法给对话框注册确定按钮,当点击确定按钮后,就会调用ActivityCollector的finishAll()方法销毁所有活动,并重新进入到LoginActivity界面。因为是在广播接收者中启动活动,所以一定要给intent加上FLAG_ACTIVITY_NEW_TASK这个标志。最后,还要把对话框的类型设置成TYPE_SYSTEM_ALERT,不然无法在广播接收者中弹出对话框。现在还剩下在AndroidManifest.xml中进行配置的任务:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.beijing.broadcast03"> <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=".BaseActivity" /> <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"/> <receiver android:name=".ForceOfflineReceiver"> <intent-filter> <action android:name="com.beijing.broadcast03.FORCE_OFFLINE" /> </intent-filter> </receiver> </application> </manifest>
有三点需要注意:首先,在ForceOfflineReceiver中弹出了一个系统级别的对话框,需要为其声明Android.permission.SYSTEM_ALERT_WINDOW权限;其次要把LoginActivity注册为主活动;最后再注册ForceOfflineReceiver,并指定它接收com.beijing.broadcast03.FORCE_OFFLINE这条广播。
现在运行程序看看效果,先进入登录界面,并且可以输入帐号和密码:
输入帐号admin和密码123456后点击Login按钮就会进到主界面:
继续点击发送广播的按钮,就会发送一条广播。ForceOfflineReceiver收到这条广播后会弹出一个提示已被强制下线的对话框:
这时,用户将无法再做任何操作,只能点击确定按钮,重新回到登录界面。到此,通过广播接收者强制下线的目标已经完成了。
加载全部内容