Sending a running application to background programmatically

前端 未结 4 730
礼貌的吻别
礼貌的吻别 2020-12-16 18:50

Is it possible to send a activity into background programmatically in android?

I am creating a prank application that plays funny sounds after a specified time (inpu

相关标签:
4条回答
  • 2020-12-16 18:55

    This function ultimately works for you

    moveTaskToBack(true)
    

    Or download source code . Android minimize app programmatically

    0 讨论(0)
  • 2020-12-16 19:01

    To expand on the answer by @ns476, the reason you should play it from a service is that any Activity that is no longer in the foreground can be killed at any time by the OS. Please review the activity lifecycle.

    0 讨论(0)
  • 2020-12-16 19:03

    Maybe play the sound from a service instead?

    0 讨论(0)
  • 2020-12-16 19:08

    Yes.

    You can use either:

    boolean sentAppToBackground = moveTaskToBack(true);
    
    if(!sentAppToBackground){
      Intent i = new Intent();
      i.setAction(Intent.ACTION_MAIN);
      i.addCategory(Intent.CATEGORY_HOME);
      this.startActivity(i);
    }
    

    More information here: http://developer.android.com/reference/android/app/Activity.html#moveTaskToBack(boolean)

    Or simply:

    Intent i = new Intent();
    i.setAction(Intent.ACTION_MAIN);
    i.addCategory(Intent.CATEGORY_HOME);
    this.startActivity(i);
    

    According to Romain Guy a Android Framework Engineer, "You cannot simulate a press on the Home key.". So beware...

    Check: http://osdir.com/ml/Android-Developers/2010-03/msg01887.html

    Updated this answer according to: moveTaskToBack(true) returns false always

    0 讨论(0)
提交回复
热议问题