Today, I saw a person writing code. There are several strange methods. As follows:
Public boolean onKeyDown (int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
If (keyCode = KeyEvent. KEYCODE_BACK ){
AlertDialog. Builder builder = new AlertDialog. Builder (this );
Builder. setMessage ("are you sure you want to exit? ")
. SetCancelable (false)
. SetPositiveButton ("OK ",
New DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog,
Int id ){
MyLocation. this. finish ();
// The following methods are strange. I have never used them so far,
Android. OS. Process
. KillProcess (android. OS. Process
. MyPid (); // This method is relatively easy to find. I have not found the following two
Android. OS. Process
. KillProcess (android. OS. Process
. MyTid ());
Android. OS. Process
. KillProcess (android. OS. Process
. MyUid ());
}
})
. SetNegativeButton ("return ",
New DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog,
Int id ){
Dialog. cancel ();
}
});
AlertDialog alert = builder. create ();
Alert. show ();
Return true;
}
Return super. onKeyDown (keyCode, event );
}
Below is a piece of information found on the Internet:
When I encountered a problem in a working program that needed to completely shut down the application, I found a lot of articles on the network, and each article uses System. exit (0) or android. OS. process. killProcess (android. OS. process. myPid (), but I have tried it, System. exit (0) does not work at all, but android. OS. process. killProcess (android. OS. process. myPid () can only close the current Activity, that is, it is effective for an application that only has a single Activity. If there are many external activities, it will be powerless.
The following describes how to completely close applications of multiple activities:
The following methods are provided in the ActivityManager class:
/**
* Have the system perform a force stop of everything associated
* The given application package. All processes that share its uid
* Will be killed, all services it has running stopped, all activities
* Removed, etc. In addition, a {@ link Intent # ACTION_PACKAGE_RESTARTED}
* Broadcast will be sent, so that any of its registered alarms can * be stopped, notifications removed, etc.
*
* You must hold the permission * {@ link android. Manifest. permission # RESTART_PACKAGES} to be able
* Call this method.
*
* @ Param packageName The name of the package to be stopped.
*/
Public void restartPackage (String packageName ){
Try {
ActivityManagerNative. getDefault (). restartPackage (packageName );
}
Catch (RemoteException e ){}
}
To close the entire application, you only need to run the following two lines of code:
ActivityManager activityMgr = (ActivityManager) this. getSystemService (ACTIVITY_SERVICE );
ActivityMgr. restartPackage (getPackageName ());
Finally, you need to add this permission:
<! -- Disable application permissions -->
<Uses-permission android: name = "android. permission. RESTART_PACKAGES"/>
Many users may find that their Android program has A lot of activities, such as the main window A, calling subwindow B, and how to close the entire Android Application in B? Here are three simple methods for implementation.
First, you need to describe how to use finish () in B. Next, the phone displays the main window A, so let's take A look at how it is implemented.
1. Local Dalvik VM Method
Android. OS. process. killProcess (android. OS. process. myPid () // obtain the PID. Currently, only this API is available for obtaining the PID. Otherwise, enumerate other processes from/proc, you may not have the permission to end other processes.
System. exit (0); // standard exit method of general java and c #. If the returned value is 0, the System Exits normally.
2. Task Manager Method
First, it must be noted that this method runs at the Android 1.5 API Level 3 or above, and the permission android. permission. RESTART_PACKAGES: You can directly end your package by using the restartPackage method of the ActivityManager class. The parameter is package name, which is passed through getSystemService (Context. ACTIVITY_SERVICE) to instantiate the ActivityManager object. This method is provided by the system, but the declared permissions need to be displayed.
3. According to the Activity declaration cycle
We know that the window class of Android provides A historical stack, and we can implement it skillfully through the stack principle. Here we add the Intent icon directly to window A when window A opens window B. FLAG_ACTIVITY_CLEAR_TOP. When B is enabled, all activities in the process space are cleared.
Use the following code to call window B in window:
Intent intent = new Intent ();
Intent. setClass (Android123.this, CWJ. class );
Intent. setFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP); // pay attention to the FLAG settings of the row.
StartActivity (intent );
Next, use the finish method to exit all the services in window B.
========================================================== =
1. Local Dalvik VM Method
Java code
Android. OS. Process. killProcess (android. OS. Process. myPid () // obtain the PID,
// Or
System. exit (0); // standard exit method of general java and c #. If the returned value is 0, the System Exits normally.
2. Task Manager Method
First, it must be noted that this method runs at the Android 1.5 API Level 3 or above, and the permission android. permission. RESTART_PACKAGES: You can directly end your package by using the restartPackage method of the ActivityManager class. The parameter is package name, which is passed through getSystemService (Context. ACTIVITY_SERVICE) to instantiate the ActivityManager object. This method is provided by the system, but the declared permissions need to be displayed.
3. According to the Activity declaration cycle
We know that the window class of Android provides A historical stack, and we can implement it skillfully through the stack principle. Here we add the Intent icon directly to window A when window A opens window B. FLAG_ACTIVITY_CLEAR_TOP. When B is enabled, all activities in the process space are cleared.
Use the following code to call window B in window:
Java code
Intent intent = new Intent ();
Intent. setClass (Android123.this, CWJ. class );
Intent. setFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP); // pay attention to FLAG settings
StartActivity (intent );
Next, use the finish method to exit all the services in window B.
4. Exceptional exit of Manufacturing
First, we can create a null pointer exception, such as TextView. the setText method executes an int-type content, because the setText method overload R. string. int-type content of a resource such as xxx, but we did not declare this resource. If we only write String as the int value, an exception will occur, at this time, the Dalvik VM will directly shut down your process. However, some netizens have said what to do with the Force Close dialog box. In fact, we can rewrite the Application base class of the Android Application to implement Thread by ourselves. the UncaughtExceptionHandler interface's uncaughtException method can avoid FC windows. The user feels the same as exiting directly.
After testing, method 1 can only end the current activity but cannot completely exit the program. Method 2 does not work either. Maybe it is not used correctly. But if you do not have much permission to grant to the application, will the user not be suspicious during installation? Method 3 is actually the same as finishing the current activity before the jump. If the previous activity needs to load a large amount of resources and information, the user experience will be poor. Method 4 is not tested and theoretically feasible.
Here we provide two methods:
1. Register a specific Broadcast in each Activity. When the program ends, the Broadcast will be sent. After all the unfinished activities are received, finish themselves.
2. Simulate an activity Stack:
Java code
Package info. wegosoft. android. util;
Import java. util. ArrayList;
Import java. util. List;
Import android. app. Activity;
/**
* Filename: ActivityStackControlUtil. java
* Package: info. wegosoft. android. util
* Description: activity stack management class. This class is added whenever an activity is generated. When an activity is finished, it is removed.
* When you exit the program completely, you only need to call the finishProgram method to end the program completely.