[FMX] Switches the Android app to the background and switches from the background to the foreground implementation

Source: Internet
Author: User
Tags switches

Sometimes we need to switch our Android program to run in the background and switch it to the foreground if necessary. The following provides a way to do this by first referencing three cells:

1 Uses androidapi. JNI. App,androidapi. JNI. Graphicscontentviewtext,androidapi. Helpers;

To switch the application to the background, there are two ways of switching the app back to the desktop, which is equivalent to pressing the Home key after execution:

12345678910 procedure sendapptoback(Sender: tobject); var Intent:jintent; beginIntent: = tjintent. Create; Intent. Setaction(tjintent. Javaclass. Action_main); Intent. SetFlags(tjintent. Javaclass. Flag_activity_new_task); Intent. Addcategory(tjintent. Javaclass. Category_home); sharedactivitycontext. StartActivity(intent); end;

Another option is to call the Jactivitymanager.movetasktoback function, which is equivalent to pressing the return key, but the program does not exit. First we need to implement a Activitymanager service instance, XE7 does not provide a default function, we imitate write a, this function in the back of the program to the foreground when the need to use:

1234567 function sharedactivitymanager: jactivitymanager; var aservice: jobject; beginaservice := sharedactivitycontext. Getsystemservice (tjcontext. Javaclass. Activity_service Result : = tjactivitymanager. Wrap((aservice as ilocalobject). Getobjectid); end;

Now switch to the background, just one line of code:

1234 procedure sendapptoback; begin Sharedactivitymanager. Movetasktoback(sharedactivity. GetTaskID,tjintent. Javaclass. Flag_activity_new_task); end;

In turn, switching to the foreground requires only a single line of code:

delphi/pascal
1234 procedure Bringapptofront begin sharedactivitymanager. Movetasktofront (sharedactivity.gettaskid,tjintent.< Span class= "crayon-v" >javaclass. Flag_activity_new_task end;

With Movetasktoback/movetasktofront, it is important to note that the application needs to be authorized reorder tasks (check it out in the project settings).

[FMX] Switches the Android app to the background and switches from the background to the foreground implementation

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.