android wear sdk introduction

77
Introduction to the SDK Tiziano Basile Mike Trizio

Upload: tiziano-basile

Post on 15-Jul-2015

240 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Android wear SDK introduction

Introduction to the SDK

Tiziano Basile

MikeTrizio

Page 2: Android wear SDK introduction

#androidwear

Page 3: Android wear SDK introduction
Page 4: Android wear SDK introduction

#androidwear

Page 5: Android wear SDK introduction

#androidwear

Page 6: Android wear SDK introduction

#androidwear

Page 7: Android wear SDK introduction

#androidwear

Page 8: Android wear SDK introduction

#androidwear

Page 9: Android wear SDK introduction

you talk to the wearable

#androidwear

Page 10: Android wear SDK introduction

actions

#androidwear

Page 11: Android wear SDK introduction

actions

#androidwear

Page 12: Android wear SDK introduction

the wearable talks to you

actions

#androidwear

Page 13: Android wear SDK introduction

actions

context

#androidwear

Page 14: Android wear SDK introduction

actions

context

#androidwear

Page 15: Android wear SDK introduction

Launched automatically

#androidwear

Page 16: Android wear SDK introduction

Glanceable

#androidwear

Page 17: Android wear SDK introduction

Suggest and demand

#androidwear

Page 18: Android wear SDK introduction

Zero or low interaction

#androidwear

Page 19: Android wear SDK introduction

#androidwear

Page 20: Android wear SDK introduction

Notifications Apps

#androidwear

Page 21: Android wear SDK introduction

Notifications

#androidwear

Page 22: Android wear SDK introduction

#androidwear

Page 23: Android wear SDK introduction

#androidwear

NO WORK REQUIRED

Page 24: Android wear SDK introduction

#androidwear

Page 25: Android wear SDK introduction

#androidwear

NO WORK REQUIRED

Page 26: Android wear SDK introduction

RepliesPagesStacks

#androidwear

Page 27: Android wear SDK introduction

Apps

#androidwear

Page 28: Android wear SDK introduction

Send data Custom UI Voice Actions

#androidwear

Page 29: Android wear SDK introduction

#androidwear

Page 30: Android wear SDK introduction

Node

Data

Message

#androidwear

Page 31: Android wear SDK introduction

Send data Custom UI Voice Actions

#androidwear

Page 32: Android wear SDK introduction

#androidwear

Page 33: Android wear SDK introduction

#androidwear

Page 34: Android wear SDK introduction

dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.google.android.support:wearable:+' compile 'com.google.android.gms:play-services-wearable:+'}

build.gradle

#androidwear

Page 35: Android wear SDK introduction

Classes

#androidwear

● BoxInsetLayout

● Card Fragment

● CircledImageView

● ConfirmationActivity

● DismissOverlayView

● GridViewPager

● GridPagerAdapter

● FragmentGridPagerAdapter

● WatchViewStub

Page 36: Android wear SDK introduction

Send data Custom UI Voice Actions

#androidwear

Page 37: Android wear SDK introduction

#androidwear

Page 38: Android wear SDK introduction

Available commands

#androidwear

● Call a car/taxi

● Take a note

● Set alarm

● Set timer

● Start/Stop a bike ride

● Start/Stop a run

● Start/Stop a workout

● Show heart rate

● Show step count

Page 39: Android wear SDK introduction
Page 40: Android wear SDK introduction

Apps

Page 41: Android wear SDK introduction

Notifications Stand alone Apps

Page 42: Android wear SDK introduction

Just edit you handheld app’s code

and you’re ready to go!

Notifications

Page 43: Android wear SDK introduction

Add build.gradle dependencies

compile "com.android.support:support-v4:20.0.+"

Import support library’s classes

import android.support.v4.app.NotificationCompat;import android.support.v4.app.NotificationManagerCompat;import android.support.v4.app.NotificationCompat.WearableExtender;

build.gradle

Page 44: Android wear SDK introduction

Create and issue the notification

Notification mNotification = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Simple Notification") .setContentText("Just a title, a text and an icon") .build();

NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(this);mNotificationManager.notify(mNotificationId, mNotification);

MobileActivity.java

Page 45: Android wear SDK introduction

Add a “open on device” action

Intent intent = new Intent(this, NotificationIntentActivity.class);intent.putExtra("EventID", 1);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

Notification mNotification = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Simple Notification") .setContentText("Just a title, a text and an icon") .setContentIntent(pendingIntent) .build();

MobileActivity.java

Page 46: Android wear SDK introduction

Add a custom action

Intent intent = new Intent(Intent.ACTION_VIEW);Uri position = Uri.parse("geo:0,0?q=41.109388,16.878843");intent.setData(position);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

Notification mNotification = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Simple Notification") .setContentText("Just a title, a text and an icon") .addAction(R.drawable.ic_location, "Check your position", pendingIntent); .build();

MobileActivity.java

Page 47: Android wear SDK introduction

Add a custom action ONLY ON WEAR

NotificationCompat.Action mAction = new NotificationCompat.Action.Builder( R.drawable.ic_location, “Check your position” mPendingIntent).build();

NotificationCompat.WearableExtender mExtender = new NotificationCompat.WearableExtender() .addAction(mAction);

Notification mNotification = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Simple Notification") .setContentText("Just a title, a text and an icon") .extend(mExtender) .build();

MobileActivity.java

Page 48: Android wear SDK introduction

WearableExtender

NotificationCompat.WearableExtender mExtender = new NotificationCompat.WearableExtender() .addAction(mAction) .setBackground(myBitmap) //set a background image .setHintHideIcon(true); //hide the notification icon on the card

Further info about the WearableExtender class at http://bit.ly/1sA2DsL

MobileActivity.java

Page 49: Android wear SDK introduction

Voice Inputs

● Handled by the Cue Card

● Can provide a list of predefined commands

● Use RemoteInput class to request user

interaction via voice commands

Page 50: Android wear SDK introduction

Prepare Voice Input

public static final String EXTRA_VOICE_REPLY = “user voice”;

public String[] mChoices = getResources().getStringArray(R.array.choices);

RemoteInput mVoiceInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY) .setLabel(“Reply”) .setChoices(choices) //optional .build();

...let’s add it to a notification

MobileActivity.java

Page 51: Android wear SDK introduction

Prepare Voice Input

NotificationCompat.Action mVoiceAction = new NotificationCompat.Action.Builder(R.drawable.icon, “Reply”, mPendingIntent) .addRemoteInput(mVoiceInput) .build();

NotificationCompat.WearableExtender mExtender = new NotificationCompat.WearableExtender() .addAction(mVoiceAction);

Notification mNotification = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle(R.string.notification_title) .setContentText(“Here’s the notification text”) .extend(mExtender) .build();

MobileActivity.java

Page 52: Android wear SDK introduction

Receive Voice Input

@Overridepublic void onCreate(Bundle onSavedInstanceState){ super.onCreate(onSavedInstanceState); Intent mReceivedIntent = getIntent(); Bundle mRemoteInput = RemoteInput.getResultsFromIntent(mReceivedIntent); String mreply = “”; if(mRemoteInput != null){ mReply = mRemoteInput.getCharSequence(EXTRA_VOICE_REPLY).toString(); }}

MobileActivity.java

Page 53: Android wear SDK introduction

Paged Notifications

NotificationCompat.Builder mFirstPageBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.icon) .setContentTitle(R.string.title) .setContentText(“This is the first page”);

Notification mSecondPage = new NotificationCompat.Builder(this) .setContentTitle(R.string.titletwo) .setContentText(“This is the second page!”) .build();

Notification mNotification = new NotificationCompat.WearableExtender() .addPage(mSecondPage) .extend(mFirstPage) .build();

MobileActivity.java

Page 54: Android wear SDK introduction

Stacked Notifications

public static final String APP_GROUP = “App Notifications”;

Notification mNotification = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Simple Notification") .setContentText("Just a title, a text and an icon") .setGroup(APP_GROUP) .build();

MobileActivity.java

Page 55: Android wear SDK introduction

Stacked Notifications, GroupSummary

public static final String APP_GROUP = “App Notifications”;

Notification mSummaryNotification = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setLargeIcon(largeIcon) .setStyle(new NotificationCompat.InboxStyle() .addLine("First Notification Hi, I'm a notification") .addLine("Second Notification Yay, here's the second one!") .setBigContentTitle("2 Notifications from your app") .setSummaryText("com.example.app")) .setGroup(GROUP_ID) .setGroupSummary(true) .build();

MobileActivity.java

Page 56: Android wear SDK introduction

Write your wear apps almost like

phone apps

Stand Alone Apps

Page 57: Android wear SDK introduction

● Access to device’s sensors

● A wearable app is more useful in some

situations

● Quick info without pulling out the phone

Stand Alone Apps

Page 58: Android wear SDK introduction

● Short Timeline

● Needs a companion app because of Playstore

compatibility

● Less functionality than a phone app

● DON’T PORT YOUR UI ON A WEARABLE!!!

Stand Alone Apps

Page 59: Android wear SDK introduction

android.webkitandroid.printandroid.app.backupandroid.appwidgetandroid.hardware.usb

no support on wear

Don’t use these packages

Page 60: Android wear SDK introduction

How to create a wearable app?

Page 61: Android wear SDK introduction

Phase 1: Prepare your devices

● Pair your phone with wear/emulator

● Enable Bluetooth debug in wear app

● Enabe Bluetooth debug on the wear device

● Enable Developer Options on wear (as easy as on the phone)

● Connect the phone to the PC

● Open terminal and launch one of the following commands

adb -s <phone id> forward tcp:5601 tcp:5601

adb forward tcp:4444 localabstract:/adb-hub; adb connect localhost:4444

Page 62: Android wear SDK introduction

Phase 2: create a project

Page 63: Android wear SDK introduction

● Notifications:

● Data Layer:

● UI:

If you don’t need to share notification between mobile and wear, use the standard notification class, otherwise use NotificationCompat

Use only if you need to sync data or send messages between mobile and wear. Remove if not necessary

You should use it if you want to create beautiful UI that really rocks!Docs available at http://bit.ly/1p8ekRu

Choose your libraries

Page 64: Android wear SDK introduction

<activity android:name=”com.app.NotificationActivity” android:exported=”true” android:allowEmbedded=”true” android:taskAffinity=”” android:theme=”@android:style/Theme.DeviceDefault.Light”>

</activity>

Use Activity as Custom Notification

.Manifest

Page 65: Android wear SDK introduction

Intent mNotificationIntent = new Intent(this, NotificationActivity.class);PendingIntent mNotificationPendingIntent = PendingIntent.getActivity( this, 0, mNotificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Notification mNotification = new Notification.Builder(this) .setSmallIcon(R.drawable.icon) //Mandatory .setContentTitle(R.string.title) .extend(new Notification.WearableExtender() .setDisplayIntent(mNotificationPendingIntent)) .build();

WearActivity.java

Use Activity as Custom Notification

Page 66: Android wear SDK introduction

Built-in intents

Custom intents

Freestyle

Add Voice Commands

Page 67: Android wear SDK introduction

Built-in intents

Just add an intent filter in your Activity

Add Voice Commands

Page 68: Android wear SDK introduction

Built-in Intents

<activity android:name=”.NoteActivity”> <intent-filter> <action android:name=”android.intent.action.SEND”/> <category android:name=”com.google.android.voicesearch.SELF_NOTE” /> </intent-filter></activity>

A complete list of built-in intents can be found at http://bit.ly/1p8omlo

Add Voice Commands

.Manifest

Page 69: Android wear SDK introduction

Custom intents

Add an attribute in the activity tag in manifest

Add Voice Commands

Page 70: Android wear SDK introduction

<activity android:name=”.NoteActivity” android:label=”NoteApp”> <intent-filter> <action android:name=”android.intent.action.MAIN”/> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter></activity>

The user can launch the app saying “Launch NoteApp”

Custom Intents

Add Voice Commands

.Manifest

Page 71: Android wear SDK introduction

Freestyle

use startActivityForResult()

Add Voice Commands

Page 72: Android wear SDK introduction

private static final int SPEECH_REQ_CODE = 1;......Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_FORM_FREE_FORM);startActivityForResult(speechIntent, SPEECH_REQ_CODE);

Freestyle

Add Voice Commands

.WearActivity

Page 73: Android wear SDK introduction

@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data){ if(requestCode == SPEECH_REQ_CODE && resultCode == RESULT_OK){ List<String> results = data.getStringArrayListExtra( RecognizerIntent.EXTRA_RESULTS); String spokenText = results.get(0); } super.onActivityResult(requestCode, resultCode, data);}

.WearActivity

Freestyle

Add Voice Commands

Page 74: Android wear SDK introduction

https://github.com/tizionario/AndroidWearSDKDemo

Try it yourself!

Page 75: Android wear SDK introduction

What’s next?Kickstart your experience with Android WearMario Viviani

Advanced Dev Tips for Android WearAlfredo Morresi

Page 76: Android wear SDK introduction

TomorrowFit4Dev - Case StudyNicola Policoro & Marco Rinaldi14:50 @ Sala Lisbona

Page 77: Android wear SDK introduction

Thank you!please leave a feedback

Tiziano Basile (@tizionario)[email protected]

Mike Trizio (@mik3lantoni0)[email protected]