How to open the main app from wearable?

I play a little with wearables, I already created notifications from my micro application (which works on wearables ) with some slight limitations and I am wondering how I can create pending intent for an action to open the main application on the phone.

+6
source share
2 answers

My solution that I just tested and it works is to add a message listener to the Android app and just send from the downloadable details what I want to do.

public class WearMessagesAPI_Service extends WearableListenerService { private static final String OPEN_APP_PATH = "/OpenApp"; @Override public void onMessageReceived(MessageEvent event) { Log.w("WearMessagesAPI_Service", event.getPath()); String activityKey = new String(event.getData()); if(activityKey.equals(...)) { Intent myAppIntent = ... create intent ... startActivity(myAppIntent); } } } 

Remember to add it to your manifest:

 <service android:name=".wearable.WearMessagesAPI_Service"> <intent-filter> <action android:name="com.google.android.gms.wearable.BIND_LISTENER"/> </intent-filter> </service> 

And I believe that it is. Let me know how this happens :)

+3
source

I do not know if there is another way.

However, it can work. You create a notification in your wear module that starts the broadcast in wear. The broadcast (which works on wear) uses Message.API to send a message to the mobile module. There is a WearableListenerService on the mobile module that launches MainActivity on the mobile device.

In Wear, create a notification:

  // Notification NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) //.setContentTitle(mNotificationUtil.getTitle()) .setContentText("Text") .setContentIntent(getPendingIntent(this)); // Get an instance of the NotificationManager service NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); // Build the notification and issues it with notification manager. notificationManager.notify(1, notificationBuilder.build()); } private PendingIntent getPendingIntent(MainActivity mainActivity) { final String INTENT_ACTION = "it.gmariotti.receiver.intent.action.TEST"; Intent intent = new Intent(); intent.setAction(INTENT_ACTION); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); return pendingIntent; } 

This notification triggers a broadcast message. Announce this broadcast in your clothes /AndroidManifest.xml

wear /AndroidManifes.xml

 <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <receiver android:name=".MyBroadcast"> <intent-filter> <action android:name="it.gmariotti.receiver.intent.action.TEST"/> </intent-filter> </receiver> 

Then broadcast to send the message:

 public class MyBroadcast extends BroadcastReceiver implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { Node mNode; // the connected device to send the message to GoogleApiClient mGoogleApiClient; private static final String WEAR_PATH = "/hello-world-wear"; @Override public void onReceive(Context context, Intent intent) { //Connect the GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(context) .addApi(Wearable.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); mGoogleApiClient.connect(); } /** * Send message to mobile handheld */ private void sendMessage() { if (mNode != null && mGoogleApiClient!=null && mGoogleApiClient.isConnected()) { Wearable.MessageApi.sendMessage( mGoogleApiClient, mNode.getId(), WEAR_PATH, null).setResultCallback( new ResultCallback<MessageApi.SendMessageResult>() { @Override public void onResult(MessageApi.SendMessageResult sendMessageResult) { if (!sendMessageResult.getStatus().isSuccess()) { Log.e("TAG", "Failed to send message with status code: " + sendMessageResult.getStatus().getStatusCode()); } } } ); }else{ //Improve your code } } /* * Resolve the node = the connected device to send the message to */ private void resolveNode() { Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() { @Override public void onResult(NodeApi.GetConnectedNodesResult nodes) { for (Node node : nodes.getNodes()) { mNode = node; } sendMessage(); } }); } @Override public void onConnected(Bundle bundle) { resolveNode(); } @Override public void onConnectionSuspended(int i) { //Improve your code } @Override public void onConnectionFailed(ConnectionResult connectionResult) { //Improve your code } } 

This code will send a message to your mobile phone. This requires wear / build.gradle

 dependencies { compile "com.google.android.support:wearable:1.0.+" compile 'com.google.android.gms:play-services-wearable:+' } 

On the mobile module you must implement WearableListenerService **

 /** * @author Gabriele Mariotti ( gabri.mariotti@gmail.com ) */ public class ListenerServiceFromWear extends WearableListenerService { private static final String WEAR_PATH = "/hello-world-wear"; @Override public void onMessageReceived(MessageEvent messageEvent) { /* * Receive the message from wear */ if (messageEvent.getPath().equals(WEAR_PATH)) { Intent startIntent = new Intent(this, MainActivity.class); startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(startIntent); } } } 

The Mobile /AndroidManifest.xml ad must declare a Service.

 <service android:name=".ListenerServiceFromWear"> <intent-filter> <action android:name="com.google.android.gms.wearable.BIND_LISTENER" /> </intent-filter> </service> 

and this mobile / build.gradle dependency :

 dependencies { wearApp project(':wear') compile 'com.google.android.gms:play-services-wearable:+' } 
+5
source

Source: https://habr.com/ru/post/975401/


All Articles