2

I am developping an application that uses a remote Service that we bought to manage the navigation of our trucks. So we bind to the service and we send him several commands as launch navigation etc, and the service answers threw a ServiceConnection. (TRemoteServiceConnection). I Have a procedure OnHandleMessage in which I receive the result of the commands from the service. On a result of the service, The service is in the foreground and I Would like to put my application to the foreground. So I was thinking about using an intent this way but nothing happens:

        Intent := TJIntent.Create;
        Intent.setClassName(StringToJString('com.embarcadero.And_PTV'), StringToJString('com.embarcadero.firemonkey.FMXNativeActivity'));
        Intent.setAction(TJIntent.JavaClass.ACTION_MAIN);
        Intent.addCategory(TJIntent.JavaClass.CATEGORY_LAUNCHER);
        Intent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_REORDER_TO_FRONT);
        SharedActivityContext.startActivity(Intent);

I also tried other flags as "FLAG_ACTIVITY_REORDER_TO_FRONT" or "FLAG_ACTIVITY_CLEAR_TOP" or "FLAG_ACTIVITY_BROUGHT_TO_FRONT" but it still doesn't work. However with the flag "FLAG_ACTIVITY_NEW_TASK" my app launches but its a new instance of the activity that starts and I don't want it.

  • if you are calling this code from a `Service`, `Intent.FLAG_ACTIVITY_NEW_TASK` is what you need. This should not start another instance of your `Activity`, it should just bring the existing task to the foreground. You may be seeing this nasty Android bug: http://stackoverflow.com/a/16447508/769265 To determine if you are seeing this bug, force quit your app and then start it again by clicking the app icon on the HOME screen. Then your `Service` should be able to bring your app to the foreground without creating another `Activity` instance. – David Wasser Jan 04 '17 at 21:26
  • Thank you for your answer. I am not calling this intent from a Service but from my application, My application receives a message from the service but my app is in the background and the service is in the foreground – Florian Wident Jan 05 '17 at 08:28
  • What version of Android are you testing on? There was a problem that prevented this from bringing an app to the foreground if you call `startActivity()` on an `Activity` `Context` (which you are doing). Try calling `startActivity()` on the global application `Context` and see if that helps. – David Wasser Jan 05 '17 at 08:48
  • The version of android is the 4.4.3 and the sdk version is 24.3.3, I try your solution and I get back to you – Florian Wident Jan 05 '17 at 09:40
  • Do you know how to get the global application Context? I am beginning with JNI. `TAndroidHelper.Context.startActivity(Intent);` Is like `SharedActivityContext.startActivity(Intent);` – Florian Wident Jan 05 '17 at 09:47
  • I don't know Delphi/Firemonkey but based on what I've seen, it looks like `TAndroidHelper.Context` is the application global `Context`. – David Wasser Jan 05 '17 at 09:52
  • Is this code called from inside an `Activity`? If so, you may need to call `startActivity` from inside a `Service` or `BroadcastReceiver` instead. – David Wasser Jan 05 '17 at 09:53
  • If you can't get that to work,, an alternative solution would be, instead of using a "launch `Intent`", to just start a new `Activity` that does nothing. This would bring your existing task to the foreground and start the new `Activity` on top. The new `Activity` would do nothing an go away, leaving your task in the foreground in the state it was in when it was last pushed to the background. See http://stackoverflow.com/a/7286683/769265 for details of this solution. – David Wasser Jan 05 '17 at 09:55
  • I tried with TAndroidHelper.Context but it's still the same result . Yeah the code is called from inside the activity AND_PTV, and I try to get the same app( AND_PTV ) to the foreground. To explain you better, My application (AND_PTV ) launches a service ( PTV_NAVIGATOR) and the service sends me a message when the navigation is stopped by the user ( at this moment, the service is still at the foreground), and I catch this message in my app AND_PTV and I want my application to get back to the foreground – Florian Wident Jan 05 '17 at 10:08
  • How does the `Service` send your app a message? – David Wasser Jan 05 '17 at 11:00
  • I bind to the service at the beginning : `FServiceConnection.BindService('com.ptvfr.trucknavigation.app', 'com.ptvag.navigation.ri.RIService');` And I Send my messages using a Service messenger and I Specify the ReplyTo property so that the service know where to answer : `MessageAEnvoyer :=TJMessage.JavaClass.obtain(nil,MSG_RI_REGISTER_EVENT_LISTENER); MessageAEnvoyer.replyTo := FServiceConnection.LocalMessenger;` The service is not developped by myself, but when the event that I Registered to triggers,he used the replyTo property to send me a message the same way I send him – Florian Wident Jan 05 '17 at 11:08
  • And I Catch the incoming message of my app on a procedure OnHandleMessage, it's in this code that I want my application to get to the foreground. I Tested your solution to start an empty activity, It starts the empty activity and gets back to the service – Florian Wident Jan 05 '17 at 11:13
  • After looking for answers on topics, I Found that an other way to get the Application context is calling : `TAndroidHelper.Activity.getBaseContext.startActivity(Intent);` Instead of `SharedActivityContext.startActivity(Intent);` and it works !!! But I don't understand why, do you know? – Florian Wident Jan 05 '17 at 12:31
  • Sorry, I don't know enough about Delphi or FireMonkey. I use native Android, so I know how it works under the covers, but I don't know anything about how they have implemented this framework. – David Wasser Jan 05 '17 at 12:38
  • I also do not understand your comment regarding my alternative solution. What do you mean that after the empty `Activity` is launched it "gets back to the `Service`"? A `Service` has no UI, so an `Activity` cannot return to it. – David Wasser Jan 05 '17 at 12:40
  • I created a projet called 'Activite_Vide', So I changed the Intent to start 'Activite_vide' instead of 'and_ptv' , it launched 'activite_vide' , then 'activite_vide' finishes and so android gets back to the screen that is the most at the foreground : the service. ( and not to my application) Was it the thing to do? Can you tell me what is the difference between the activity context and the global application context? It seems like it was the problem – Florian Wident Jan 05 '17 at 12:54
  • Ah. In Android the term `Service` means something completely different. We're having terminology problems. Anyway, The difference between `Context` objects in Android cannot be explained in 500 characters in a comment. If you are really interested search here on Stackoverflow for "[android] difference context activity application" and read up. It is kinda complicated. – David Wasser Jan 05 '17 at 17:19
  • There is a bug in Android that prevents an `Activity` from bringing its own task to the foreground by calling `startActivity()`. This was fixed in Android 4.4, so I don't understand why you are seeing this problem. See this for more info: http://stackoverflow.com/questions/29766270/how-to-resume-android-activity-programmatically-from-background/29769255#29769255 – David Wasser Jan 05 '17 at 18:06

0 Answers0