Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to launch google maps app in the background from my app

First note, I am using Xamarin for Android, so there are minor syntax differences to Android Java. I am launching the google maps app from an activity in my own Android app. I have figured out how to launch it in turn by turn navigation mode giving voice guided directions, skipping the regular activity that it normally defaults to. However, the default map activity does come up very briefly before then going right into turn by turn activity. I can live with that.

The challenge is that the user has to click the back button multiple times to get back to my app, rather than once where it goes from turn by turn activity, straight back to my app, which is what I would prefer. I know why it's doing that and it's natural. Is there any way for my app to launch the map in voice guided mode, but launch it in the background keeping my app in the foreground?

I have seen that if I use my back button to return to my app, the turn by turn voice directions stop. If I use the window icon at phone bottom to see all my recent apps and bring mine to the foreground after launching the maps app, the voice directions keep working, which is what I want, and it even gives me notification overlays over top of my app which is great. I don't want to have to tell the user to use that windows icon when he wants to bring my app into the foreground, and I don't want him using the back button because it will stop the voice directions. So it's a tough spot and i wouldn't be surprised if this is just one of those phone behavior things you have to live with, but I'm hoping anybody can offer any suggestions. Maybe even making it part of a fragment app? But would I just get their maps with that or would I get the full voice directions feature as well? I thought I could only get that from their installed app, not their library. Thanks for reading.

Here's my current code

public void StartNavigation(string lat, string lng)
{
    String strUri = "google.navigation:q=" + lat + "," + lng;
    Intent Intent = new Intent(Android.Content.Intent.ActionView, Android.Net.Uri.Parse(strUri));
    //Intent.SetClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
    Intent.SetPackage("com.google.android.apps.maps");
    Application.Context.StartActivity(Intent);
}
like image 810
Bobh Avatar asked Apr 22 '17 18:04

Bobh


People also ask

Can I integrate Google Maps in my app?

Open Android Studio, and click Create New Project in the Welcome to Android Studio window. In the New Project window, under the Phone and Tablet category, select the Google Maps Activity, and then click Next. Complete the Google Maps Activity form: Set Language to Java or Kotlin.


2 Answers

Perhaps you can solve that using Intent flags to change the launch mode of the Google Maps Activity.

Try to use the FLAG_ACTIVITY_NEW_TASK flag. e.g.:

String strUri = "google.navigation:q=" + lat + "," + lng;
Intent intent = new Intent(Android.Content.Intent.ActionView, Android.Net.Uri.Parse(strUri));
//Intent.SetClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
Intent.SetPackage("com.google.android.apps.maps");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Application.Context.StartActivity(Intent);
like image 73
Mauker Avatar answered Oct 12 '22 02:10

Mauker


A few seconds after starting Google Maps, fire a launcher intent for your own app to bring it back to the foreground. You may need to use an alarm for this.

like image 26
StackOverthrow Avatar answered Oct 12 '22 03:10

StackOverthrow