Last active
September 15, 2020 11:33
-
-
Save gabrielemariotti/117b05aad4db251f7534 to your computer and use it in GitHub Desktop.
Android Wear: small gist to start an Activity on the mobile handheld from the Android Wear device.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<service android:name=".ListenerServiceFromWear"> | |
<intent-filter> | |
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" /> | |
</intent-filter> | |
</service> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
wearApp project(':wear') | |
compile 'com.google.android.gms:play-services-wearable:+' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ListenerServiceFromWear extends WearableListenerService { | |
private static final String HELLO_WORLD_WEAR_PATH = "/hello-world-wear"; | |
@Override | |
public void onMessageReceived(MessageEvent messageEvent) { | |
/* | |
* Receive the message from wear | |
*/ | |
if (messageEvent.getPath().equals(HELLO_WORLD_WEAR_PATH)) { | |
Intent startIntent = new Intent(this, MyActivity.class); | |
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
startActivity(startIntent); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
compile "com.google.android.support:wearable:1.0.+" | |
compile 'com.google.android.gms:play-services-wearable:+' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LaunchActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { | |
Node mNode; // the connected device to send the message to | |
GoogleApiClient mGoogleApiClient; | |
private static final String HELLO_WORLD_WEAR_PATH = "/hello-world-wear"; | |
private boolean mResolvingError=false; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_launch); | |
//Connect the GoogleApiClient | |
mGoogleApiClient = new GoogleApiClient.Builder(this) | |
.addApi(Wearable.API) | |
.addConnectionCallbacks(this) | |
.addOnConnectionFailedListener(this) | |
.build(); | |
//UI elements with a simple CircleImageView | |
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub); | |
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() { | |
@Override | |
public void onLayoutInflated(WatchViewStub stub) { | |
CircledImageView mCircledImageView = (CircledImageView) stub.findViewById(R.id.circle); | |
//Listener to send the message (it is just an example) | |
mCircledImageView.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
sendMessage(); | |
} | |
}); | |
} | |
}); | |
} | |
/** | |
* Send message to mobile handheld | |
*/ | |
private void sendMessage() { | |
if (mNode != null && mGoogleApiClient!=null && mGoogleApiClient.isConnected()) { | |
Wearable.MessageApi.sendMessage( | |
mGoogleApiClient, mNode.getId(), HELLO_WORLD_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 | |
} | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
if (!mResolvingError) { | |
mGoogleApiClient.connect(); | |
} | |
} | |
/* | |
* 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; | |
} | |
} | |
}); | |
} | |
@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 | |
} | |
} |
Thank you for this nice example. Works like a charm!
But please keep in mind: The Manifest IntentFilter BIND_LISTENER is now deprecated. You should not use:
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
anymore.
More informations here: http://android-developers.blogspot.de/2016/04/deprecation-of-bindlistener.html
The service declaration in the Manifest should look like this now:
<service android:name=".ListenerServiceFromWear">
<intent-filter>
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
<data android:scheme="wear" android:host="*"
android:path="/hello-world-wear" />
</intent-filter>
</service>
This is the only working app currently on google which call handheld activity from wearable. Thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I got burned for a week trying to get this to work only to find out the wear app and mobile app in android studio need to have THE SAME EXACT PACKAGE NAME or the messages won't get received. Great gist!