Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Android Wear

Introduction to Android Wear

An introduction to Android Wear

Gabriele Mariotti

October 03, 2014

Transcript

70 slides
  1. What is Android Wear ? Android Wear extends the Android

    platform to a new generation of wearable devices
  2. How does it work ? • Phone and wearable communicate

    with each other over BT • Require Android 4.3+ on phone/tablet • Wearable itself doesn’t connect to internet • Limited function when connection is lost • Current available sensors are Compass, Accelerometer, Gyroscope, Heart Rate Sensor(*)
  3. JAVA Notification : basic example (1) // Notification NotificationCompat.Builder notificationBuilder

    = new NotificationCompat.Builder(mContext) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle(“Notification Title”) .setContentText(“Notification Text”) .setContentIntent(mPendingIntent);
  4. JAVA Notification : basic example (2) // Get an instance

    of the NotificationManager service NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mContext); // Build the notification and issues it with notification manager. notificationManager.notify(notificationId, notificationBuilder.build());
  5. JAVA Notification : long text //BigText NotificationCompat.BigTextStyle bigStyle = new

    NotificationCompat.BigTextStyle(); bigStyle.setBigContentTitle(“Big Title”); bigStyle.bigText(“Lorem ipsum……..”); // Notification NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext) .setSmallIcon(R.drawable.ic_launcher).setStyle(bigStyle) .setContentIntent(mPendingIntent);
  6. JAVA Notification : big picture //BigPicture NotificationCompat.BigPictureStyle bigStyle = new

    NotificationCompat.BigPictureStyle(); bigStyle.bigPicture (BitmapFactory.decodeResource(mContext.getResources(),R.drawable.large_image)); bigStyle.setBigContentTitle(“Big Title”); bigStyle.setSummaryText(“Notification Text”); // Notification NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext) .setSmallIcon(R.drawable.ic_launcher).setStyle(bigStyle) .setContentIntent(mPendingIntent);
  7. JAVA NotificationCompat.BigPictureStyle bigStyle = new NotificationCompat.BigPictureStyle(); bigStyle.bigPicture (BitmapFactory.decodeResource(mContext.getResources(), R.drawable.large_image)); bigStyle.setBigContentTitle(“Second

    Page”); // Create second page notification Notification secondPageNotification = new NotificationCompat.Builder(mContext) .setStyle(bigStyle) .build(); Notification: pages
  8. JAVA // Notification NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle(“Notification

    Title”) .setContentText(“Notification Text”) .extend( new NotificationCompat.WearableExtender().addPage(secondPageNotification)) .setContentIntent(mPendingIntent); Notification: pages
  9. JAVA // Notification NotificationCompat.Builder notificationBuilder1 = new NotificationCompat.Builder(mContext) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle(“Notification

    Title”) .setContentText("Alex New meeting") .setGroup(GROUP_KEY_EMAILS) .setContentIntent(mPendingIntent) .setSortKey("0"); notificationManager.notify(notificationId, notificationBuilder1.build()); Notification: stacks (1)
  10. JAVA NotificationCompat.Builder notificationBuilder2 = new NotificationCompat.Builder(mContext) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle(“Notification Title”) .setContentText("Peter

    Next week") .setGroup(GROUP_KEY_EMAILS) .setContentIntent(mPendingIntent) .setSortKey("1"); notificationManager.notify(notification2Id, notificationBuilder2.build()); Notification: stacks (2)
  11. JAVA // Create an InboxStyle notification Notification summaryNotificationWithBackground = new

    NotificationCompat.Builder(mContext) .setSmallIcon(R.drawable.ic_launcher).setLargeIcon(largeIcon) .setStyle(new NotificationCompat.InboxStyle() .addLine("Alex New meeting") .addLine("Peter Next week") .setBigContentTitle("2 new messages") .setSummaryText("[email protected]")) .setGroup(GROUP_KEY_EMAILS).setGroupSummary(true).build(); notificationManager.notify(notification3Id, summaryNotificationWithBackground); Notification: stacks (3)
  12. JAVA // Notification NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle(“Notification

    Title”) .setContentText(“Notification Text”) .addAction(R.drawable.ic_full_action, “Simple Action”,mPendingActionIntent) .setContentIntent(mPendingIntent); Notification: simple action
  13. JAVA NotificationCompat.Action action = new NotificationCompat.Action.Builder( R.drawable.ic_full_reply,“Reply”,mPendingIntent) .addRemoteInput(mNotificationUtil.getReplyAction()) .build(); public

    RemoteInput getReplyAction(){ RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY) .setLabel(“Reply”).build(); return remoteInput; } Notification: reply action (1)
  14. JAVA // Notification NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle(“Notification

    Title”) .setContentText(“Notification Text”) .extend(new NotificationCompat.WearableExtender().addAction(action)); Notification: reply action (2)
  15. JAVA public RemoteInput getReplyActionWithPredefinedTextResponses(){ RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY) .setLabel(“Reply”)

    .setChoices(mContext.getResources().getStringArray(R.array.reply_choices)) .build(); return remoteInput; } <string-array name="reply_choices"> <item>Yes</item> <item>No</item> </string-array> Notification: reply action with choices
  16. JAVA GoogleApiClient Connection //Connect the GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)

    .addApi(Wearable.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build();
  17. JAVA GoogleApiClient Connection @Override protected void onStart() { super.onStart(); mGoogleApiClient.connect();

    } @Overrire protected void onStop(){ super.onStop(); mGoogleApiClient.disconnect(); }
  18. JAVA Connected Nodes Connection private void resolveNode() { Wearable.NodeApi.getConnectedNodes(mGoogleApiClient) .setResultCallback(new

    ResultCallback<NodeApi.GetConnectedNodesResult>() { @Override public void onResult(NodeApi.GetConnectedNodesResult nodes) { for (Node node : nodes.getNodes()) { //….. } } }); }
  19. JAVA Node - Listeners Connection public interface NodeApi.NodeListener { void

    onPeerConnected(com.google.android.gms.wearable.Node node); void onPeerDisconnected(com.google.android.gms.wearable.Node node); } Example: public class MyActivity extends Activity implements NodeApi.NodeListener { public void onConnected(Bundle bundle) { Wearable.NodeApi.addListener(mGoogleApiClient, this); } }
  20. JAVA Wearable.MessageApi.sendMessage( mGoogleApiClient, nodeId, PATH, payload).setResultCallback( new ResultCallback<MessageApi.SendMessageResult>() { @Override

    public void onResult(MessageApi.SendMessageResult sendMessageResult) { //………. } } ); Message MessageAPI
  21. JAVA Message - Listener Message interface MessageApi.MessageListener { void onMessageReceived(com.google.android.gms.wearable.MessageEvent

    messageEvent); } Example: public class MyActivity extends Activity implements MessageApi.MessageListener { public void onConnected(Bundle bundle) { Wearable.MessageApi.addListener(mGoogleApiClient, this); } }
  22. JAVA PutDataMapRequest dataMap = PutDataMapRequest.create("/count"); dataMap.getDataMap().putInt(COUNT_KEY, count++); //dataMap.getDataMap().putString(“/myItemId”, “Hello World”);

    PutDataRequest request = dataMap.asPutDataRequest(); PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi.putDataItem(mGoogleApiClient, request); Data DataAPI
  23. JAVA Data - Listener Data interface DataApi.DataListener { void onDataChanged(com.google.android.gms.wearable.DataEventBuffer

    dataEventBuffer); } Example: public class MyActivity extends Activity implements DataApi.DataListener { public void onConnected(Bundle bundle) { Wearable.DataApi.addListener(mGoogleApiClient, this); } }
  24. JAVA Activity Receiving Message public classs MyActivity extends Activity implements

    MessageApi.MessageListener { //Add the listener in onConnected() method @Override public void onMessageReceived(MessageEvent messageEvent) { if (messageEvent.getPath().equals(PATH)) { byte[] bytes = messageEvent.getData(); } } }
  25. JAVA Service Receiving Message <service android:name=".MyListenerService"> <intent-filter> <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />

    </intent-filter> </service> public classs MyListenerService extends WearableListenerService { @Override public void onMessageReceived(MessageEvent messageEvent) { //………….. }
  26. JAVA Activity Receiving Data public classs MyActivity extends Activity implements

    DataApi.DataListener { //Add the listener in onConnected() method @Override public void onDataChanged(DataEventBuffer dataEvents) { for (DataEvent event : dataEvents) { if (event.getType() == DataEvent.TYPE_DELETED) { Log.d(TAG, "DataItem deleted: " + event.getDataItem().getUri()); } else if (event.getType() == DataEvent.TYPE_CHANGED) { Log.d(TAG, "DataItem changed: " + event.getDataItem().getUri()); } } }
  27. JAVA Service Receiving Data <service android:name=".MyListenerService"> <intent-filter> <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />

    </intent-filter> </service> public classs MyListenerService extends WearableListenerService { @Override public void onDataChanged(DataEventBuffer dataEvents) { //………….. }
  28. JAVA Example: launch Activity on mobile from wear SendMessageActivity extends

    Activity { … @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Wearable.API) .build(); } wear/SendMessageActivity.java
  29. JAVA Example: launch Activity on mobile from wear … @Override

    protected void onStart() { super.onStart(); mGoogleApiClient.connect(); } @Override protected void onStop() { super.onStop(); mGoogleApiClient.disconnect(); } wear/SendMessageActivity.java
  30. JAVA Example: launch Activity on mobile from wear private void

    resolveNode() { Wearable.NodeApi.getConnectedNodes(mGoogleApiClient) .setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() { @Override public void onResult(NodeApi.GetConnectedNodesResult nodes) { for (Node node : nodes.getNodes()) { sendMessage(node); } } }); } wear/SendMessageActivity.java
  31. JAVA Example: launch Activity on mobile from wear static final

    String HELLO_WORLD_WEAR_PATH = "/hello-world-wear"; private void sendMessage(Node node){ Wearable.MessageApi.sendMessage( mGoogleApiClient, node.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()); } } } ); } wear/SendMessageActivity.java
  32. JAVA Example: launch Activity on mobile from wear public class

    ListenerServiceFromWear extends WearableListenerService { private static final String WEAR_PATH = "/hello-world-wear"; @Override public void onMessageReceived(MessageEvent messageEvent) { if (messageEvent.getPath().equals(WEAR_PATH)) { Intent startIntent = new Intent(this, EmptyActivity.class); startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(startIntent); } } } mobile/ListenerService.java
  33. JAVA Example: launch Activity on mobile from wear You can

    find the full code here: https://gist.github.com/gabrielemariotti/117b05aad4db251f7534
  34. References and Credits - Getting Started with Android Wear http://developer.android.com/wear

    - Build apps for Wearables http://developer.android.com/training/building-wearables.html - Thanks to Mario Viviani for some slides and ideas from Kick-Start Your Experience with Android Wear

More Decks by Gabriele Mariotti

Other Decks in Technology