Last active
August 29, 2015 14:03
-
-
Save takahirom/322dcdeb1959f057982f to your computer and use it in GitHub Desktop.
Wear対応アプリをスマホでアプリ起動時に、WearでActivityを開始する
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
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(); | |
} |
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" /> | |
<service android:name=".MyWearableListenerService"> | |
<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
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