Hide and exit Android applications

Source: Internet
Author: User

 

To hide the program when you press the Home key:

1: Before Android 2.0, you need to listen to the button event to determine if the back key is pressed.

2: After Android 2.0, the system provides an onbackpressed method, which is used to listen for back-key events. Therefore, you only need to override the onbackpressed method.

@ Override

Public void onbackpressed {

// Implement the home key effect

// Super. onbackpressed; this sentence must be noted out, otherwise the default back processing method will be called.

Intent I = "" New intent (intent. action_main );

I. setflags (intent. flag_activity_new_task );

I. addcategory (intent. category_home );

Startactivity (I );

Exit application implementation: you can write a method by yourself, for example:

Public void exitprogrames {

Intent startmain = new intent (intent. action_main );

Startmain. addcategory (intent. category_home );

Startmain. setflags (intent. flag_activity_new_task );

Startactivity (startmain );

Android. OS. process. killprocess (Android. OS. process. mypid); // here you can also use finsh () to only killprocess to kill processes more thoroughly than finsh (). finsh () is exited by calling the destoyt () method of activity!

There is also a popular Android classic perfect exit method, which creates an activity management object in singleton mode. This object has an activity container (which can be processed by itself, and uses consumer list) it stores every newly opened activity, which is easy to understand and operate!
Myapplication class (stores every activity and closes all activities)
Public class myapplication extends application {

Private list <activity> activitylist = new shortlist <activity> ();
Private Static myapplication instance;

Private myapplication ()
{
}
// Obtain the unique myapplication instance in singleton Mode
Public static myapplication getinstance ()
{
If (null = instance)
{
Instance = new myapplication ();
}
Return instance;
}
// Add the activity to the container
Public void addactivity (activity)
{
Activitylist. Add (activity );
}
// Traverse all activities and finish
Public void exit ()
{
For (activity: activitylist)
{
Activity. Finish ();
}
System. Exit (0 );
}
}
Add the activity to the container of the myapplication object instance in the oncreate method of each activity.
Myapplication. getinstance (). addactivity (this );
Call the exit method when you need to end all activities.
Myapplication. getinstance (). Exit ();

 

I will introduce and comment on some other exit methods (if not, please correct them ):
@ Restartpackage (getpackagename () (this is not detailed)
A small software I developed in sdk2.1 cannot be exited after I put it on the android2.2 or 2.3 operating system, because the restartpackage method is no longer used in Versions later than sdk2.1, the reason is that it is not secure and may shut down the service shared with other applications. Other users need to use this service. It is wrong if you shut it down.

@ Someone said the ultimate exit method:
Intent startmain = new intent (intent. action_main );
Startmain. addcategory (intent. category_home );
Startmain. setflags (intent. flag_activity_new_task );
Startactivity (startmain );
System. Exit (0 );
In fact, this method only returns the home page. If you enter the application again, you will find that the first interface is the activity you have not previously closed.

@ Someone said that calling Android. OS. process. killprocess (Android. OS. process. mypid (); or system. Exit (0) actually only exits the current activity!

@ Call the forcestoppackage method to hide the forcestoppackage method. This method is called through ing (there are other methods)
Method method = NULL;
Activitymanager manager = (activitymanager) getsystemservice (activity_service );
Try {
Method = Class. forname ("android. App. activitymanager"). getmethod ("forcestoppackage", String. Class );
Method. Invoke (Manager, getpackagename ());
} Catch (exception e ){
Log. D ("force", E. getmessage ());
}
In sdk2.2 and 2.3, the test result is that the nullpointerexception occurs and an error window pops up. The program is forced to close, which is different from the expected normal exit. However, you can modify the base class to implement the uncaughtexception method of the thread. uncaughtexceptionhandler interface, so that no error window pops up. The program exits completely.

@ It is the same as above, but this is intentional Manufacturing of abnormal exit (above is an unintentional manufacturing exception), but I think this is the best strategy after all.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.