
Android Wear Docs, Release 1.1
for (DataEvent event : dataEvents) {
// Check the event type
if (event.getType() == DataEvent.TYPE_CHANGED) {
// Check the data path
if (path.equals(WEARABLE_START_PATH)) {
// Create a local notification
dataMap = DataMapItem.fromDataItem(event.getDataItem()).getDataMap();
sendLocalNotification(dataMap);
}
}
}
}
3. In the wearable, implement the procedure that constructs and posts a demand (notification) that can launch your
app. Optionally, the Pending Intent in this notification can include extra data for the wearable app.
private void sendLocalNotification(DataMap dataMap) {
int notificationId = 001;
// Create a pending intent that starts this wearable app
Intent startIntent = new Intent(this, HoleActivity.class).setAction(Intent.ACTION_MAIN);
// Add extra data for app startup or initialization, if available
startIntent.putExtra("extra", dataMap.getString("extra"));
PendingIntent startPendingIntent =
PendingIntent.getActivity(this, 0, startIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notify = new NotificationCompat.Builder(this)
.setContentTitle(dataMap.getString("title"))
.setContentText(dataMap.getString("body"))
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentIntent(startPendingIntent)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, notify);
}
The wearable notification stack now includes a notification inviting the user to launch your wearable app. A swipe to
the left displays the launcher icon, which the user clicks to launch the app.
4. In the wearable app, receive and process any extra information. Normally, you implement this within the
onCreate override of your app.
// Check for extra data in the intent
// If present, extract and use
Bundle extras = getIntent().getExtras();
if (extras != null) {
// Get the extra data
String extraData = extras.getString("extra");
...
// Act on the extra data
...
}
5.3. Handheld Activation 23
Comentarios a estos manuales