Skip to content Skip to sidebar Skip to footer

How To Launch Android Wear Activity From Mobile

I have been working on a project where I need a button on a mobile to start up an activity on the watch. I have been going through the data layer sample in the sdk but can not get

Solution 1:

Moblie

Accessing the Wearable Data Layer

MainActivity.java

publicclassMainActivityextendsActivityimplementsGoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
privateGoogleApiClient mGoogleApiClient;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mGoogleApiClient = newGoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mGoogleApiClient.connect();
}

@OverridepublicvoidonConnected(Bundle bundle) {
    Log.d("GoogleApi", "onConnected: " + bundle);
}

@OverridepublicvoidonConnectionSuspended(int i) {
    Log.d("GoogleApi", "onConnectionSuspended: " + i);
}

@OverridepublicvoidonConnectionFailed(ConnectionResult connectionResult) {
    Log.d("GoogleApi", "onConnectionFailed: " + connectionResult);
}

GetConnectedNodes

Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
                @Override
                public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) {
                    for (Node node : getConnectedNodesResult.getNodes()) {
                        sendMessage(node.getId());
                    }
                }
            });

SendMessage

publicstatic final StringSTART_ACTIVITY_PATH = "/start/MainActivity";


privatevoidsendMessage(String node) {
        Wearable.MessageApi.sendMessage(mGoogleApiClient , node , START_ACTIVITY_PATH , new byte[0]).setResultCallback(newResultCallback<MessageApi.SendMessageResult>() {
            @OverridepublicvoidonResult(MessageApi.SendMessageResult sendMessageResult) {
                if (!sendMessageResult.getStatus().isSuccess()) {
                    Log.e("GoogleApi", "Failed to send message with status code: "
                            + sendMessageResult.getStatus().getStatusCode());
                }
            }
        });
    }

Wear

Implement a Message Listener

WearDataLayerListenerService.java

publicclassWearDataLayerListenerServiceextendsWearableListenerService {
publicstaticfinalStringSTART_ACTIVITY_PATH="/start/MainActivity";
@OverridepublicvoidonMessageReceived(MessageEvent messageEvent) {
    super.onMessageReceived(messageEvent);
    if(messageEvent.getPath().equals(START_ACTIVITY_PATH)){
        Intentintent=newIntent(this , MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}}

Add Listener Service to Manifest

<serviceandroid:name=".WearDataLayerListenerService"><intent-filter><actionandroid:name="com.google.android.gms.wearable.BIND_LISTENER" /></intent-filter></service>

Post a Comment for "How To Launch Android Wear Activity From Mobile"