How to hide intent in android? - android

I have an application with the google map usage. In my case I'm opening the google map, outside from an activity. I did the following,
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri
.parse("http://maps.google.com/maps?daddr=" + latitude + "," + longitude + "&mode=driving"));
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationIntent.setPackage("com.google.android.apps.maps");
notificationIntent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
context.startActivity(notificationIntent);
But when I'm opening the map It shows over my application. I need to avoid the showing of map on my application. In other words my app should be the top application. Is there a way to do this?

Related

How can I enforce returning to my app when starting google navigation with an intent

I call Google Maps with an Intent like this:
Intent mapIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" + lat + "," + lon + "&mode=d"));
mapIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
Now I want to return to my app automatically when arriving at the destination. How can I achieve that?
Bonus question: Can I retrieve some information from Maps about the route like duration?

How to show distance between two points on map

I have latitude and longutude of two points. I want to show the route between them by opening google maps from my app on button click. How to do so?
Use below code for getting show route in google map...
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("
http://maps.google.com/maps?" +"saddr=" + sourcelatLng + "&daddr=" + destlatlng;
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
If you want to open maps app with a route from your source to destination, you just need to start an activity with following intent.
Uri routeUri = Uri.parse("http://maps.google.com/maps?saddr=your-lat-1,your-lng-1&daddr=your-lat-2,your-lng-2");
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, routeUri);
startActivity(intent);
Replace your-lat/lng-1/2 with your latitude and longitude.

Android intent for opening both Waze and Google maps

There are a few similar posts but I couldn't find an exact one. basically, I want to open both Google maps and Waze with the same intent. At first I tried this:
final String uri = String.format(Locale.ENGLISH, "geo:%f,%f", latitude, longitude);
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
Waze navigated directly to the right location and Google maps opened the right place. then I realized that Google maps doesn't put a pin on the location so it's hard for the user to know where it is exactly. So I looked around and realized that Google maps requires the "?q=..(label)" for that... I changed the uri construction to:
final String uri = String.format(Locale.ENGLISH, "geo:%f,%f?q=%f,%f (%s)", latitude, longitude, latitude, longitude, name);
But then Waze did 2 things: navigated to the right place AND run a search on the label. This required the user to click the back button to close the search results screen and remain with the navigation to the right place.
I looked everywhere for an answer but failed to find a solution that will achieve both.
At first I thought that it's not possible and Waze has a bug... but then I noticed that Facebook messenger is doing exactly what I want. when clicking on a message with a location it will open both apps: Google maps will have a pin (with a label) and Waze will navigate straight to that location without running a search.
Few questions about the above:
1. (Of course) How can I achieve that?
2. How can I know how the Facebook messenger's intent being built? (Can I catch it in anyway)
3. What's the reason behind having the label only with the "?q=.."?
Thanks
Never mind. I was able to intercept the Facebook messenger with the below app and figured that the URI should be as follows:
String.format(Locale.ENGLISH, "geo:0,0?q=") + android.net.Uri.encode(String.format("%s#%f,%f", label, latitude, longitude), "UTF-8");
The app:
https://play.google.com/store/apps/details?id=uk.co.ashtonbrsc.android.intentintercept&feature=search_result#?t=W251bGwsMSwyLDEsInVrLmNvLmFzaHRvbmJyc2MuYW5kcm9pZC5pbnRlbnRpbnRlcmNlcHQiXQ..
Thanks
Following Nimrod's tip I've installed the app and intercepted the intent from whatsapp's location feature. Here's the full Intent tested on maps and waze:
String uri = "http://maps.google.com/maps?q=loc:"+latitude+","+longitude+" ("+label+")";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
intent.setFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
intent.setData(Uri.parse(uri));
startActivity(intent);
My final solution for opening both Waze & GoogleMap applications to get direction ( works like a charm ) :
String uri = "";
Intent intent;
try {
uri = String.format(Locale.ENGLISH,"geo:" + location.getLat() + "," +location.getLng() + "?q=" + location.getLat()+","+location.getLng()+" ("+location.getName()+")");
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} catch (ActivityNotFoundException ex) {
try {
Intent unrestrictedIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(unrestrictedIntent);
} catch (ActivityNotFoundException innerEx) {
getSnackbar(getResources().getString(R.string.map_install_application), Snackbar.LENGTH_LONG).show();
}
}
I had the same problem - wanted the navigation app (whatever it is) to show a pin (preferably with a custom label) and be ready to navigate the user there. However, having a label in the query resulted in some apps running a search or (worse) assuming the closest address match to the label and showing that as the destination (ignoring the coordinates). So, to achieve a reliable behaviour I had to forget having a named label and ended up with the following:
final String geoIntentData = "geo:" + latitude + "," + longitude + "?q=" + latitude + "," + longitude;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(geoIntentData));
So far it works in all apps I tested (Google Maps, Waze, Sygic, Moovit and even Komoot and Windy Maps). Please let me know if you try this solution and it doesn't work in another app!

Voice Guidance for Google Maps Route in Android

My application calls Google Maps via an Intent to show best route between two points using this code:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?dirflg=d&saddr=" + String.valueOf(currentPosition.getPosition().latitude) + "," + String.valueOf(currentPosition.getPosition().longitude) + "&daddr=" + String.valueOf(currentDest.getPosition().latitude) + "," + String.valueOf(currentDest.getPosition().longitude)));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
I was wondering, is it possible to activate voice guidance for the route via a parameter in the URL?
Use this code for voice guidance and navigation
Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse("google.navigation:q="
+Destination));
startActivity(i);

Open Inbuilt Map Intent For Driving Direction

I want to open an inbuilt google map intent to show route between two place without open a complete action dialog, which asked for Browser and Map .
I tried this.
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr="+PreferenceData.getLatitude(HotelInfomation.this)+","+PreferenceData.getLongitude(HotelInfomation.this)+"&daddr="+latitude+","+longitude));
startActivity(intent);
But it opens a dialog asking Complete Action Using Browser or map . I don't want that Dialog .
without open a complete action dialog, which asked for Browser and Map
In this case you need to tell android which application you want to use to display map.
Here you go
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr="+PreferenceData.getLatitude(HotelInfomation.this)+","+PreferenceData.getLongitude(HotelInfomation.this)+"&daddr="+latitude+","+longitude));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
Intent intent = new Intent(
android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr="
+ Data.map_latitude + ","
+ Data.map_longitude + "+&daddr="
+ Data.infoLatitude + ","
+ Data.infoLogitute + ""));

Categories

Resources