"Resolved" How Android makes apps run in the background

Source: Internet
Author: User

applications running in the background, this argument may not be accurate, that is, the application does not Finish exit, but also not in the foreground state, such as app execution clicked on Home The same key. How to achieve it?

Points:

back in the background is executed Home Key, Activity executed separately. OnPause and the OnStop, The app is not destroyed, back to the background, run the app again as soon as it executes Onresume you can do it.

Complete exit, execute finish,activity will execute onpause,onstop and the OnDestroy, the application is destroyed, it will be reclaimed by the system, run the application again to execute onCreate start again.

1. perform the home key

Private ResolveInfo Homeinfo;

public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);

Packagemanager pm = Getpackagemanager ();

Homeinfo =pm.resolveactivity (Newintent (Intent.action_main). Addcategory (Intent.category_home), 0);

// Job Description

// Job Description

Gotoidle ();

}

private void Gotoidle () {

Activityinfo AI =homeinfo.activityinfo;

Intent startintent= New Intent (Intent.action_main);

Startintent.addcategory (Intent.category_launcher);

Startintent.setcomponent (New ComponentName (Ai.packagename,

Ai.name));

Startactivitysafely (startintent);

}

voidstartactivitysafely (Intent Intent) {

Intent.addflags (Intent.flag_activity_new_task);

try {

StartActivity (Intent);

} catch (Activitynotfoundexception e) {

Toast.maketext (This, "work wrongly",

Toast.length_short). Show ();

} catch (SecurityException e) {

Toast.maketext (This, "notsecurity", Toast.length_short). Show ();

LOG.E (TAG, "Launcher does not having the permission to launch"

+ Intent

+ ". Make sure to create a MAIN intent-filter for the corresponding activity "

+ "Oruse The exported attribute for this activity.",

e);

}

}

2.back Key Rewrite execution Home key Function

public boolean onKeyDown (int keycode, keyevent event) {

if (KeyCode ==keyevent.keycode_back) {

Gotoidle ();

return true;

} else

Returnsuper.onkeydown (KeyCode, event);

}

3. rewrite finish (),

@Override

public void Finish () {

//super.finish ();//activity Will never quit automatically, but in the background.

Movetasktoback (TRUE);

}


Add:

I do the sensor data collection, when the Program interface exit (press the back key, or press the Home key ), the data will not be output. So I try to make the following simple changes, to achieve the ideal effect, so share here.

The previous code was not modified:

protected void OnStop () {super.onstop (); Sm.unregisterlistener (this);}

Modified code (to achieve your desired effect):

protected void OnStop () {super.onstop ();//sm.unregisterlistener (this);  Comment out the line to ensure that the sensor listener is not logged off on exit}



"Resolved" How Android makes apps run in the background

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.