Post on 15-Jul-2015
transcript
Introduction to the SDK
Tiziano Basile
MikeTrizio
#androidwear
#androidwear
#androidwear
#androidwear
#androidwear
#androidwear
you talk to the wearable
#androidwear
actions
#androidwear
actions
#androidwear
the wearable talks to you
actions
#androidwear
actions
context
#androidwear
actions
context
#androidwear
Launched automatically
#androidwear
Glanceable
#androidwear
Suggest and demand
#androidwear
Zero or low interaction
#androidwear
#androidwear
Notifications Apps
#androidwear
Notifications
#androidwear
#androidwear
#androidwear
NO WORK REQUIRED
#androidwear
#androidwear
NO WORK REQUIRED
RepliesPagesStacks
#androidwear
Apps
#androidwear
Send data Custom UI Voice Actions
#androidwear
#androidwear
Node
Data
Message
#androidwear
Send data Custom UI Voice Actions
#androidwear
#androidwear
#androidwear
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.google.android.support:wearable:+' compile 'com.google.android.gms:play-services-wearable:+'}
build.gradle
#androidwear
Classes
#androidwear
● BoxInsetLayout
● Card Fragment
● CircledImageView
● ConfirmationActivity
● DismissOverlayView
● GridViewPager
● GridPagerAdapter
● FragmentGridPagerAdapter
● WatchViewStub
Send data Custom UI Voice Actions
#androidwear
#androidwear
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
Apps
Notifications Stand alone Apps
Just edit you handheld app’s code
and you’re ready to go!
Notifications
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
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
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
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
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
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
Voice Inputs
● Handled by the Cue Card
● Can provide a list of predefined commands
● Use RemoteInput class to request user
interaction via voice commands
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
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
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
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
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
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
Write your wear apps almost like
phone apps
Stand Alone Apps
● Access to device’s sensors
● A wearable app is more useful in some
situations
● Quick info without pulling out the phone
Stand Alone Apps
● 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
android.webkitandroid.printandroid.app.backupandroid.appwidgetandroid.hardware.usb
no support on wear
Don’t use these packages
How to create a wearable app?
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
Phase 2: create a project
● 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
<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
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
Built-in intents
Custom intents
Freestyle
Add Voice Commands
Built-in intents
Just add an intent filter in your Activity
Add Voice Commands
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
Custom intents
Add an attribute in the activity tag in manifest
Add Voice Commands
<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
Freestyle
use startActivityForResult()
Add Voice Commands
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
@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
https://github.com/tizionario/AndroidWearSDKDemo
Try it yourself!
What’s next?Kickstart your experience with Android WearMario Viviani
Advanced Dev Tips for Android WearAlfredo Morresi
TomorrowFit4Dev - Case StudyNicola Policoro & Marco Rinaldi14:50 @ Sala Lisbona
Thank you!please leave a feedback
Tiziano Basile (@tizionario)tiz.basile@gmail.com
Mike Trizio (@mik3lantoni0)mikelantonio.trizio@gmail.com