Use the broadcast mechanism to implement the program's forced exit.
In front of a blog post, wrote a key to exit, where the use of the activitycollector there and baseactivity
Function: After opening the program, login, log in successfully, go to the main page, the main page has a button, click on the pop-up window, the user click "Yes" and then exit the program
1, Activitycollector
package com.yuanlp.forceexit;/** * created by Yuriping on 2017/6/28. */ import android.app.activity;import java.util.arraylist;import java.util.list;/** * Activity Collection */public class ActivityCollector { public static List<activity> activitylist=new arraylist<> (); public static void addactivity (activity activity) { Activitylist.add (activity); } public static void Remove (activity activity) { activitylist.remove (Activity); } public static void finishall () { for (activity activity:activitylist) { if (!activity.isfinishing ()) { //Determines if activity is destroyed activity.finish (); //Destroy activity } } }}2, baseactivity as the parent of the back activity, inherits from Appcomapactivitypackage com.yuanlp.forceexit;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;/** * created by 原立鹏 on 2017/6/28. */public class baseactivity extends appcompatactivity{ private myforceexitreceiver&NBSP;RECEIVER;&NBSP;&NBSP;//declares a broadcast receiver of its own internal classes @Override protected Void oncreate (bundle savedinstancestate) { Super.oncreate (savedinstancestate); Activitycollector.addactivity (This); } /** * registers a broadcast recipient when activity is at the top of the return stack, that is, when it is in focus */ Public void onresume () { super.onresume (); intentfilter intentfilter=new intentfilter (); // Instantiate a intent filter intentfilter.addaction ("Com.yuanlp.exit.FORCEXIT //only cares about the radio I want receiver=new Myforceexitreceiver (); reGisterreceiver (receiver,intentfilter); //Dynamic registered broadcast } Public void onpause () { //When the current activity is occupied by a popup, unregister super.onpause (); if (receiver!=null) { unregisterreceiver (receiver); receiver=null; } } public void ondestroy () { super.ondestroy (); Activitycollector.finishall (); } /** * a broadcast recipient, after receiving the broadcast, the popup prompts the user to log back in, forcing the exit */ private class myforceexitreceiver extends broadcastreceiver{ @Override public void onreceive (final context context, final intent intent) { Alertdialog.builder builder=new alertdialog.builder (context); builder.settitle ("Warning"); builder.setmessage ("You are forced to quit, please login again"); builder.setcancelable (false); //prohibit user from canceling this frame builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () { @Override public void onclick ( Dialoginterface dialog, int which) { activitycollector.finishall (); //Destroy All Activities intent intent=new intent (Context,loginactivity.class) //jump to login screen context.startactivity (Intent); } }); builder.show (); } }}
3, loginactivity login successful, jump to Mainactivity interface. Here to set the Androidmanifext.xml to this activity is launcher, that is, the first interface after opening
Package com.yuanlp.forceexit;import android.content.intent;import android.os.bundle;import android.view.view;import android.widget.edittext;public class loginactivity extends BaseActivity { private EditText mName; private edittext mpassword; @Override protected Void oncreate (bundle savedinstancestate) { Super.oncreate (savedinstancestate); setcontentview ( R.layout.activity_login); mname = (EditText) Findviewbyid (R.id.et_name); mpassword = (EditText) findviewbyid (R.id.et_password); } public void Login (View view) { string name=mname.gettext (). toString (). Trim (); string password=mpassword.gettext (). toString (). Trim (); if ("admin". Equals (name) && "123456". Equals (password)) { intent intent=new intent (This,MainActivity.class); startactivity (Intent); } }}
4, Activity_login.xml Simple one linearlayout,vertical typesetting, which put 2 horizontal layout of linearlayout, respectively put a user name and password edittext. The bottom button, implementing the Login method
<?xml version= "1.0" encoding= "Utf-8"? ><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" android:orientation= "vertical" tools:context= " Com.yuanlp.forceexit.LoginActivity "> <LinearLayout android:layout_width= "Match_parent" android: layout_height= "60DP" android:orientation= "Horizontal" > <EditText android:id= "@+id/et_name" android:layout_Width= "Wrap_content" android:layout_ height= "Wrap_content" android:layout_ weight= "1" android:ems= "ten" android:hint= "Account" android:inputtype= "Textpersonname"/> </ linearlayout> <linearlayout Android:layout_width= "Match_parent" android:layout_height= "60DP " android:orientation=" > "Horizontal" <EditText Android:id= "@+id/et_passworD " android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_weight= "1" android:ems= "Ten" android:hint= "Password" android:inputtype= "Textpersonname"/> </linearlayout> <button android:id= "@+id/button" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:onclick= "Login" android:text= "button"/></linearlayout>
5, Mainactivity
Inside the main is a button method that implements sending broadcasts.
Package Com.yuanlp.forceexit;import Android.content.intent;import Android.os.bundle;import Android.view.View; public class Mainactivity extends baseactivity {@Override protected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); } public void exit (view view) {Intent intent=new Intent ("Com.yuanlp.exit.FORCEXIT"); Sendbroadcast (Intent); Send broadcast}}
6, Activity_main.xml
<?xml version= "1.0" encoding= "Utf-8"?><android.support.constraint.constraintlayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns:app= "Http://schemas.android.com/apk/res-auto" xmlns:tools= "Http://schemas.android.com/tools " android:layout_width=" Match_parent " android:layout_height=" Match_parent " tools:context=" com.yuanlp.forceexit.MainActivity "> <button android:id= "@+id/button2" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:onclick= " Exit " android:text=" forced exit app:layout_constraintbottom_tobottomof= "Parent" app:layout_constrainttop_totopof = "Parent" tools:layout_editor_absolutex= "126DP"/></ Android.support.constraint.constraintlayout>
This article is from the "Yuangushi" blog, make sure to keep this source http://cm0425.blog.51cto.com/10819451/1942888
Android uses broadcast to implement a program's forced exit