Search code examples
androidnavigationandroid-wear-notification

Is it possible to programmatically run navigation on Android Wear?


I am making an extension for my android app. I can get the notification to android wear without a problem with all the data I want. I have lat and long for every POI for which im sending a notification, and i want to add a feature "Navigate to" inside android wear notification, that would take this coordinates and ran a google navigation on smart watch. Is this possible? Can u provide some example or documentation. Thanks

This is a sample code that i use in my app for running google maps on phone by clicking "Open on map" button on my android wear.

    Intent mapIntent = new Intent(Intent.ACTION_VIEW);
    Uri geoUri = Uri.parse("geo:0,0?q=" + chapter.getLatitude() + "," + chapter.getLongitude());
    mapIntent.setData(geoUri);
    PendingIntent mapPendingIntent =
            PendingIntent.getActivity(context, 0, mapIntent, 0);
    ...

    .addAction(R.drawable.ic_map_white_24dp, "Open on map", mapPendingIntent)

Solution

  • as far as I understand what you are trying to do, you already reached your goal. Once you are able to start navigation on your phone, it will be automatically displayed on your watch as a notification card which appears at the bottom of your watchface.

    I'm currently developing an application where I'm doing the same thing except I'm using CardFragments for triggering the action instead of using Wearable Notifications and PendingIntents. When I press the action button of the CardFragment displayed on my watch, I'm sending a Wearable Message to my Phone App which runs the following code when it receives this particular message.

    String uri = "google.navigation:q=" + String.valueOf(latitude) + "," + String.valueOf(longitude);
    Intent mapsIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
    mapsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(mapsIntent);
    

    I have to set the FLAG_ACTIVITY_NEW_TASK because the code is part of a method of a normal java class. Everything works fine when the screen of the phone is unlocked and screen is turned on. However, navigation should also start when the phone is locked. This is where I'm currently struggling with.