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