简体   繁体   中英

Android application to detect when Google maps direction is finished

I am building an android application, that will show certain points on google maps, for which I have (lat,lang) for all the points shown, with me. Whenever, a user clicks on one of these locations on google maps, I am starting a new Google Maps intent which shows navigation to the user and is supposed to take the user to that (lat, lang).

//suppose this function is called on onclick of google maps event
    function onclick(){
            Uri gmmIntentUri = Uri.parse("google.navigation:q=32.885240,-96.763475");
            Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
            mapIntent.setPackage("com.google.android.apps.maps");
            if (mapIntent.resolveActivity(getPackageManager()) != null) {
                startActivity(mapIntent);
            }
    }

This will start the navigation for the user. Now what I want to achieve is this - Whenever this navigation is complete, I want some kind of flag to be passed to my android application, so that my application knows that the navigation has been finished.

If it is possible to automatically close the google maps application after the navigation has been completed, it will be nice to have.

As @Daniel mentioned, you can use onActivityResult() to detect when users are back. However, if you really want to detect if Google maps direction had finished... You can look at the notifications.

So the catch here is, when Google Maps direction is a services, and when it is running, it posts updates to the notifications center.

Which means either you can keep looking at what services are running, or.. get permission to the notifications center and keep checking if Google Maps exists.

在此处输入图片说明

I think a better answer since you built the intent for navigation and have the coordinates, is to set up geofencing for the destination or any milestone you want in the trip. After that, you can use your IntentService to retrieve your app at the appropriate activity.

https://developer.android.com/training/location/geofencing#java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM