Instructions for Android to run the app in the background

Source: Internet
Author: User

  1. Overwrite the return key event listener, so that the back key function is similar to the home key, so that acitivty back to the background without being destroyed by the system, the code is as follows:
  2. public Span style= "margin:0px; padding:0px; Border:none ">  boolean Span style= "margin:0px; padding:0px; Border:none "> onkeydown ( int  keycode, keyevent event)  {     
  3. Packagemanager pm = Getpackagemanager ();
  4. ResolveInfo Homeinfo =
  5.              Pm.resolveactivity (new  intent (intent.action_main) addcategory (intent.category_home),   0
  6.         if   (keycode ==  Keyevent.keycode_back)  {  
  7.              activityinfo ai = homeinfo.activityinfo;    
  8.             intent  startintent = new Span style= "margin:0px; padding:0px; Border:none "> intent (intent.action_main);     
  9.              Startintent.addcategory (intent.category_launcher);     
  10.              Startintent.setcomponent (new  componentname (Ai.packagename, ai.name));     
  11.              Startactivitysafely (startintent);     
  12.             return   true ;    
  13.         } else     
  14.             return   super .onkeydown (keycode, event);     
  15.     }  
  16.      private   void  startactivitysafely (intent intent)  {    
  17. Intent.addflags (Intent.flag_activity_new_task);
  18. Try  {    
  19.              StartActivity (intent);     
  20.         } catch   (activitynotfoundexception  e)  {    
  21.              Toast.maketext (this ,  " null " Span style= "margin:0px; padding:0px; Border:none ">,    
  22.                      toast.length_short). Show ();     
  23.         } catch   (securityexception e)  {    
  24.              Toast.maketext (this ,  " null " Span style= "margin:0px; padding:0px; Border:none ">,    
  25. Toast.length_short). Show ();
  26. }
  27. }
  28. The article is reproduced from the Internet, thanks to the original author's selfless sharing.

Instructions for Android to run the app 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.