The first thing you need is a class that can contain all the activities to destroy all activities by the method that can call this object at last.
Package com.example.broadcastbestpractice;
Import java.util.ArrayList;
Import java.util.List;
Import android.app.Activity;
public class Activitycollector {
private 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 ();
}
}
}
}
All classes are required to inherit this class
Package com.example.broadcastbestpractice;
Import android.app.Activity;
Import Android.os.Bundle;
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);
}
}
Login activity
Package com.example.broadcastbestpractice;
Import android.content.Intent;
Import android.content.SharedPreferences;
Import Android.os.Bundle;
Import Android.preference.PreferenceManager;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.CheckBox;
Import Android.widget.EditText;
Import Android.widget.Toast;
Login activity
public class Loginactivity extends Baseactivity {
User name View
Private EditText Accountedit;
Password view
Private EditText Passwordedit;
Login button View
Private Button login;
Radio Box View
Private CheckBox Rememberpass;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.login);
Get the corresponding View object
Accountedit = (EditText) Findviewbyid (R.id.account);
Passwordedit = (EditText) Findviewbyid (R.id.password);
Rememberpass = (CheckBox) Findviewbyid (R.id.remember_pass);
Login = (Button) Findviewbyid (R.id.login);
Add an event for the login button
Login.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
String account = Accountedit.gettext (). toString ();
String password = Passwordedit.gettext (). toString ();
if (account.equals ("admin") && password.equals ("123456")) {
Intent Intent = new Intent (loginactivity.this, Mainactivity.class);
StartActivity (Intent);
Finish ();
} else {
Toast.maketext (Loginactivity.this,
"Account or password is invalid",
Toast.length_short). Show ();
}
}
});
}
}
Interface
<?xml version= "1.0" encoding= "Utf-8"?>
<tablelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:stretchcolumns= "1" >
<TableRow>
<textview
android:layout_height= "Wrap_content"
android:text= "User name:"/>
<edittext
Android:id= "@+id/account"
android:layout_height= "Wrap_content"
Android:hint= "Please enter user name"/>
</TableRow>
<TableRow>
<textview
android:layout_height= "Wrap_content"
android:text= "Password:"/>
<edittext
Android:id= "@+id/password"
android:layout_height= "Wrap_content"
Android:inputtype= "Textpassword"/>
</TableRow>
<TableRow>
<checkbox
Android:id= "@+id/remember_pass"
android:layout_height= "Wrap_content"/>
<textview
android:layout_height= "Wrap_content"
android:text= "Remember Password"/>
</TableRow>
<TableRow>
<button
Android:id= "@+id/login"
android:layout_height= "Wrap_content"
Android:layout_span= "2"
android:text= "Login"/>
</TableRow>
</TableLayout>
Another activity to test forced downline
Package com.example.broadcastbestpractice;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
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 Onclicklistener () {
@Override
public void OnClick (View v) {
System.out.println ("666");
Intent Intent = new Intent (
"Com.example.broadcastbestpractice.FORCE_OFFLINE");
Sendbroadcast (Intent);
}
});
}
}
Interface
<?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" >
<button
Android:id= "@+id/force_offline"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "Send Force offline broadcast"/>
</LinearLayout>
Forced Downline broadcast Receivers
Package com.example.broadcastbestpractice;
Import Android.app.AlertDialog;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import Android.content.DialogInterface;
Import android.content.Intent;
Import Android.view.WindowManager;
public class Forceofflinereceiver extends Broadcastreceiver {
@Override
public void OnReceive (final context context, Intent Intent) {
System.out.println ("555");
Eject Force downline dialog box
Alertdialog.builder Dialogbuilder = new Alertdialog.builder (context);
Dialogbuilder.settitle ("Warning");
Dialogbuilder
. Setmessage ("You is forced to be offline. Try to login again. ");
Dialogbuilder.setcancelable (FALSE);
To add an event to a dialog box button
Dialogbuilder.setpositivebutton ("OK",
New Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Destroy all activities after clicking OK
Activitycollector.finishall ();
Start logon activity
Intent Intent = new Intent (context, loginactivity.class);
SYSTEM.OUT.PRINTLN (context);
Because it is necessary to add a tag bit to start an activity in the receiver
Intent.addflags (Intent.flag_activity_new_task);
Context.startactivity (Intent);
}
});
Alertdialog Alertdialog = Dialogbuilder.create ();
//
The type of the Settings dialog box is guaranteed to pop up normally in the broadcast receiver
Alertdialog.getwindow (). SetType (
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
Alertdialog.show ();
}
}
Registration file
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.example.broadcastbestpractice"
Android:versioncode= "1"
Android:versionname= "1.0" >
<uses-sdk
Android:minsdkversion= "8"
Android:targetsdkversion= "/>"
<uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>
<application
Android:allowbackup= "true"
android:icon= "@drawable/ic_launcher"
Android:label= "@string/app_name"
Android:theme= "@style/apptheme" >
<activity
Android:name= ". Loginactivity "
Android:label= "MyLogin" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name= ". Mainactivity ">
</activity>
<receiver android:name= ". Forceofflinereceiver ">
<intent-filter>
<action android:name= "Com.example.broadcastbestpractice.FORCE_OFFLINE"/>
</intent-filter>
</receiver>
</application>
</manifest>
Android implementation QQ forced offline----from the first line of code