Skip to content

Instantly share code, notes, and snippets.

@takahirom
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takahirom/322dcdeb1959f057982f to your computer and use it in GitHub Desktop.
Save takahirom/322dcdeb1959f057982f to your computer and use it in GitHub Desktop.
Wear対応アプリをスマホでアプリ起動時に、WearでActivityを開始する
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_explain);
mGoogleApiClient = (new GoogleApiClient.Builder(this)).addApi(Wearable.API).build();
mGoogleApiClient.connect();
Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(new ResultCallback() {
public void onResult(Result result) {
onResult((NodeApi.GetConnectedNodesResult) result);
}
public void onResult(NodeApi.GetConnectedNodesResult nodesResult) {
for(Node node:nodesResult.getNodes()){
final Node finalNode = node;
sendMessage(finalNode);
}
}
});
}
private void sendMessage(final Node finalNode) {
new Thread(new Runnable() {
@Override
public void run() {
MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(mGoogleApiClient, finalNode.getId(), "/LAUNCH", null).await();
if (!result.getStatus().isSuccess()) {
Log.e(getPackageName(), "ERROR: failed to send Message: " + result.getStatus());
} else {
Log.d(getPackageName(), "SUCCESS: send Message: " + result.getStatus());
}
}
}).start();
}
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<service android:name=".MyWearableListenerService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
public class MyWearableListenerService extends WearableListenerService{
@Override
public void onMessageReceived(MessageEvent messageEvent) {
Toast.makeText(this,"Launch Notification",Toast.LENGTH_LONG).show();
if(messageEvent.getPath().equals("/LAUNCH")){
Intent intent = new Intent(this, WearExplainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment