SlideShare une entreprise Scribd logo
1  sur  51
Télécharger pour lire hors ligne
Android Wear SDK 101
⼀一⼩小時上⼿手
jollen <jollen@jollen.org>
www.mokoversity.com
創新創業分享與交流
就在 Mokoversity
Mokoversity
114年5月15⽇日星期四
• Android SDK
• ADT plugin for Eclipse v22.6.1+
• Install ‘Android Wear ARM EABI v7a System
Image’
• Use the latest version of ‘Android Support
Library’
• Set Up the Android Wear Emulator
開發環境
214年5月15⽇日星期四
• Android SDK
• Intent
• Broadcast Receiver
• Notification
• Android Design guidelines for notifications
• http://developer.android.com/guide/topics/ui/
notifiers/notifications.html
背景知識
314年5月15⽇日星期四
安裝 Android Wear SDK
414年5月15⽇日星期四
建⽴立 Android Wear Emulator
514年5月15⽇日星期四
1. Install and Use Genymotion
2. Setup ‘Nexus 5’ AVD
3. Start Android Wear emulator and Nexus 5
4. Install Android Wear Preview App on Nexus 5
5. Setup TCP port forwarding for ADB
6. Install and Open ‘Android Wear Preview’ on
Nexus 5
建⽴立 Android Wear Emulator
614年5月15⽇日星期四
$ ./adb -s [device name Nexus 5] install ./Android_Wear_Beta.apk
$ ./adb -s [device name Nexus 5] forward tcp:5601 tcp:5601
Install Android Wear Preview App
需先下載 APK 套件
714年5月15⽇日星期四
Enable notification access
permission
814年5月15⽇日星期四
連線成功、Android Wear
Emulator 啟動完成。開始第⼀一個
Android Wear App。
914年5月15⽇日星期四
Install Android Wear Preview SDK
1014年5月15⽇日星期四
import android.preview.support.wearable.notifications.*;
import android.preview.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.NotificationCompat;
Import Libraries
1114年5月15⽇日星期四
Android Wear 的本質是⼀一個
Launcher
1214年5月15⽇日星期四
•Designing for Android Wear is substantially
different than designing for phones or tablets.
•Android phones 與 tablets 是 UI、functionality 與
Touch 導向
•Android wear 是 Notification 導向
Android Wear App 的設計
1314年5月15⽇日星期四
The Context Stream
On the Android wearable, each notification
appears as a new card in the context stream.
The Cue Card
Allows users to speak to their device. There are
a list of actions. The list of actions includes
Android intents for voice actions.
UI Principles
Portions of this page are reproduced from work created and shared by the Android Open Source Project and
used according to terms described in the Creative Commons 2.5 Attribution License.
1414年5月15⽇日星期四
Step zero
從 Notification 開始
1514年5月15⽇日星期四
private void zero() {
	 	 NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
	 	 	 .setContentTitle("Zero")
	 	 	 .setContentText("You're at step zero.")
	 	 	 .setSmallIcon(R.drawable.bg_eliza);
	 	
	 	 NotificationManager mNotificationManager =
	 	 	 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
	 	
	 	 mNotificationManager.notify(0x07, builder.build());
	 }
Portions of this page are reproduced from work created and shared by the Android Open Source Project and
used according to terms described in the Creative Commons 2.5 Attribution License.
Step Zero
1614年5月15⽇日星期四
Demo
1714年5月15⽇日星期四
600x600
Icon
Image
1814年5月15⽇日星期四
private void zeroX() {
	 	 NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
	 	 	 .setContentTitle("ZeroX")
	 	 	 .setContentText("You're at step zero.")
	 	 	 .setSmallIcon(R.drawable.bg_eliza)
	 	 	 .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bg_eliza));
	 	
	 	 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);		
	 	 notificationManager.notify(0x08, builder.build());
	 }
Image
使⽤用 Large Icon for Android Wear
1914年5月15⽇日星期四
Demo
2014年5月15⽇日星期四
認識
Action
2114年5月15⽇日星期四
private void one() {
	 	 // Build an intent for an action to make a phone call
	 	 Intent phoneCallIntent = new Intent(Intent.ACTION_VIEW);
	 	 Uri phoneUri = Uri.parse("tel:119");
	 	 phoneCallIntent.setData(phoneUri);
	 	 PendingIntent phoneCallPendingIntent = PendingIntent.getActivity(this, 0, phoneCallIntent, 0);
	 	
	 	 NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
	 	 	 .setContentTitle("One")
	 	 	 .setContentText("You're at step one.")
	 	 	 .setSmallIcon(R.drawable.bg_eliza)
	 	 	 .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bg_eliza))
	 	 	 .addAction(R.drawable.ic_full_reply, "Call me", phoneCallPendingIntent);
	 	
	 	 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);		
	 	 notificationManager.notify(0x09, builder.build());
	 }
Step One
2214年5月15⽇日星期四
Put all
together.
2314年5月15⽇日星期四
Notifier
Notification
2414年5月15⽇日星期四
• NotificationCompat.Builder
Basic Android APIs
2514年5月15⽇日星期四
•Android wearables provide just the right
information at just the right time, allowing you to
be connected to the virtual world and present in
the real world.
•Android Devices (Phone/Table) 的週邊
•Android Wear 是 Notification 裝置
•Android Devices 的 Second Device
•PDA (輔具)
Android Wear 的哲學
2614年5月15⽇日星期四
•Contextually aware and smart
•Glanceable
•Zero/low interaction
•Helpful
Design Principles
2714年5月15⽇日星期四
First app
Add New Features for Wearables
2814年5月15⽇日星期四
Actions
Images
Application Icons
Pages
Notification Stacks
Voice Replies
Notification UI Patterns
2914年5月15⽇日星期四
private void appOne() {
	 	 NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
	 	 	 .setContentTitle("First App")
	 	 	 .setContentText("My first app for Android wear.")
	 	 	 .setSmallIcon(R.drawable.bg_eliza)
	 	 	 .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bg_eliza));
	 	 Notification notification = new WearableNotifications.Builder(builder)
	 	 	 .setMinPriority()
	 	 	 .build();
	 	
	 	 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);		
	 	 notificationManager.notify(0x01, notification);
	 }
Use Features for Wearables
3014年5月15⽇日星期四
private void appOneX() {
	 	 NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
	 	 	 .setContentTitle("Coupon")
	 	 	 .setContentText("Limited. 30 mins from now !")
	 	 	 .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bg_mountain));
	 	 Intent intent = new Intent(ACTION_RESPONSE);
	 	 PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent,
	 	 	 	 PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT);
	 	 builder.setContentIntent(pendingIntent);
	 	
	 	 Notification notification = new WearableNotifications.Builder(builder)
	 	 	 .setMinPriority()
	 	 	 .build();
	 	
	 	 NotificationManagerCompat.from(this).notify(0x02, notification);
	 }
	 	 	 .setHintHideIcon(true)
Use .setContentIntent()
3114年5月15⽇日星期四
Skin of Android Wear
3214年5月15⽇日星期四
Demo
3314年5月15⽇日星期四
// Broadcast receiver
	 	 mFilterWearableReply = new IntentFilter(ACTION_RESPONSE);
	 	 mWearableReceiver = new BroadcastReceiver() {
	 	 	 @Override
	 	 	 public void onReceive(Context arg0, Intent arg1) {
	 	 	 	 Toast.makeText(mContext, "Someone wants to get a coupon.", Toast.LENGTH_LONG).show();
	 	 	 }
	 	 };
	 	 registerReceiver(mWearableReceiver, mFilterWearableReply);
使⽤用 Broadcast
Receiver
3414年5月15⽇日星期四
Second & third app
Add Pages to a Notification
Voice Replies (Remote Input)
3514年5月15⽇日星期四
3614年5月15⽇日星期四
private void appTwo() {
	 	 NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
	 	 	 .setContentTitle("80% Discount")
	 	 	 .setContentText("Now products are there...")
	 	 	 .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bg_mountain));
	 	 Intent intent = new Intent(ACTION_RESPONSE);
	 	 PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent,
	 	 	 	 PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT);
	 	 builder.setContentIntent(pendingIntent);
	 	
	 	 // 1. Create a big text style for the second page
	 	 BigTextStyle secondPageStyle = new NotificationCompat.BigTextStyle();
	 	 secondPageStyle.setBigContentTitle("New Product")
	 	 .bigText("A lot of text...");
	 	 // 2. Create second page notification with big text style
	 	 Notification secondPageNotification = new NotificationCompat.Builder(this)
	 	 .setStyle(secondPageStyle)
	 	 .build();
	 	
	 	 Notification notification = new WearableNotifications.Builder(builder)
	 	 	 .setMinPriority()
	 	 	 .setHintHideIcon(true)
	 	 	 // 3. Add the second page to notification
	 	 	 .addPage(secondPageNotification)
	 	 	 .build();
	 	
	 	 NotificationManagerCompat.from(this).notify(0x01, notification);
	 }
3714年5月15⽇日星期四
private void appThree() {
	 	 NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
	 	 	 .setContentTitle("Coupon")
	 	 	 .setContentText("Limited. 30 mins from now !")
	 	 	 .setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.drawable.bg_mountain));
	 	 Intent intent = new Intent(ACTION_RESPONSE);
	 	 PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent,
	 	 	 	 PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT);
	 	 builder.setContentIntent(pendingIntent);
	 	
	 	 Notification notification = new WearableNotifications.Builder(builder)
	 	 	 .setMinPriority()
	 	 	 .setHintHideIcon(true)
	 	 	 .addRemoteInputForContentIntent(
	 	 	 	 new RemoteInput.Builder("GET !")
	 	 	 	 .setLabel("Reply").build())
	 	 	 .build();
	 	
	 	 NotificationManagerCompat.from(this).notify(0x02, notification);
	 }
3814年5月15⽇日星期四
Notification notification = new WearableNotifications.Builder(builder)
	 	 	 .setMinPriority()
	 	 	 .setHintHideIcon(true)
	 	 	 .addRemoteInputForContentIntent(
	 	 	 	 new RemoteInput.Builder("GET !")
	 	 	 	 .setLabel("Reply")
	 	 	 	 .setChoices(getResources().getStringArray(R.array.reply_choices))
	 	 	 	 .build())
	 	 	 .build();
Use .setChoices()
3914年5月15⽇日星期四
Android Intent
4014年5月15⽇日星期四
什麼是 Intent (意圖)
在 Android 應⽤用程式框架中,有⼀一個⾮非常聰明的事件處理
機制,稱之為「 Intent 」
在「 Intent 」這樣的事件處理觀念裡,Android 試圖將事
件解釋為「應⽤用程式的意圖」或是「使⽤用者的意圖」,並試
著去解釋該意圖的⺫⽬目的,若 Android 系統本⾝身能理解應⽤用
程式的意圖,便會「⾃自⾏行」去處理該意圖所應執⾏行的⼯工作
Android 的做法是,讓每個意圖(Intent)都帶有⼀一個動
作(action),並根據不同的動作去⾏行動
事件處理
4114年5月15⽇日星期四
Intent 的類型
Intent 的動作
⾃自⾏行定義
框架內部定義
Android 框架的 Intent 有很多⽅方便實⽤用的「內建動作」
Intent 定義
4214年5月15⽇日星期四
Android內建的Intent Action
Android 的框架確實是讓每個 Intent 都包含了⼀一個動作,
就稱為 action
範例:
HelloIntentDialer: 啟動撥號器 (dialer) 並撥號
HelloIntentMusic : 使⽤用者按下「 Select Music 」
後,可以由⾳音樂清單裡選擇⾳音樂並撥放
HelloIntentWallpaper : 啟動 Android 內建的「背景
圖選擇器」,讓使⽤用者更換背景
Intent Action
4314年5月15⽇日星期四
Intent 可包含 Data
除了 action 外, Intent 還可以包含另外⼀一項資訊
「data」
Intent 的 action 指定這個 Intent 的「動作」是什麼,
框架會依指定的動作進⾏行處理;有些 action 可以附帶⼀一筆
資料,這個資料是以 Uri 的格式撰寫
Intent Data
4414年5月15⽇日星期四
內建 Intent 範例
內建的 Intent 有哪些呢?請參考 Android Reference
Guide 中的 Intent 類別說明
上述三個範例分別使⽤用以下三個 action:
1. ACTION_CALL: 撥號
2. ACTION_GET_CONTENT: 啟動內容選取器
3. ACTION_SET_WALLPAPER: 設定Wallpaper
Intent 範例
4514年5月15⽇日星期四
Intent Action 的類型
Android 內建的 Intent action 分為⼆二種:
1. Activity Action : 啟動 Activity 的 action
2. Broadcast Action : 透過廣撥器處理的action
第⼀一種 action 是 activity action ,⽤用途是通知框架啟
動 Activity ,這裡提出的三個範例,都是使⽤用 activity
action
Intent 的 Action 類型
4614年5月15⽇日星期四
⾃自動撥號: HelloIntentDialer
public class HelloIntentDialer extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
 
Intent dial = new Intent();
dial.setAction("android.intent.action.CALL");
dial.setData(Uri.parse("tel:119"));
startActivity(dial);
}
}
「送出⼀一個帶有內建 action 的 Intent 給框架,因為 action 為 CALL,所以框架
會去啟動撥號 activity 並打電話。」
打電話範例
4714年5月15⽇日星期四
程式說明
1. 先產⽣生⼀一個 Intent 物件:
Intent dial = new Intent();
2. 設定 Intent 的 action 為
「 android.intent.action.CALL 」,這是⼀一個內建的
action:
dial.setAction("android.intent.action.CALL");
3. 內建 action「CALL」需要附帶⼀一筆資料,⽽而資料的寫
法是使⽤用 URI 格式:
dial.setData(Uri.parse("tel:119"));
打電話範例
4814年5月15⽇日星期四
送出 Intent
4. 「CALL」是⼀一個 activity action,所以呼叫
startActivity() 向 Intent 送給框架:
startActivity(dial);
打電話範例
4914年5月15⽇日星期四
啟動 Dialer
5014年5月15⽇日星期四
Notification, Intent & Broadcast Receiver
WearableNotificationCompat
UI Patterns
結論
5114年5月15⽇日星期四

Contenu connexe

Similaire à Android Wear SDK: Level 101

行動商務實務 - PhoneGap Advance
行動商務實務 - PhoneGap Advance行動商務實務 - PhoneGap Advance
行動商務實務 - PhoneGap Advance
My own sweet home!
 
Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天
Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天
Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天
Gelis Wu
 
透過 Windows Azure Mobile Services 開發各平台 Apps
透過 Windows Azure Mobile Services 開發各平台 Apps透過 Windows Azure Mobile Services 開發各平台 Apps
透過 Windows Azure Mobile Services 開發各平台 Apps
Eric ShangKuan
 
Javascript之昨是今非
Javascript之昨是今非Javascript之昨是今非
Javascript之昨是今非
Tony Deng
 
行動平台上利用Facebook API開發社群應用程式
行動平台上利用Facebook API開發社群應用程式行動平台上利用Facebook API開發社群應用程式
行動平台上利用Facebook API開發社群應用程式
Mu Chun Wang
 
Spring 2.x 中文
Spring 2.x 中文Spring 2.x 中文
Spring 2.x 中文
Guo Albert
 

Similaire à Android Wear SDK: Level 101 (20)

Android 智慧型手機程式設計
Android 智慧型手機程式設計Android 智慧型手機程式設計
Android 智慧型手機程式設計
 
Android应用开发 - 沈大海
Android应用开发 - 沈大海Android应用开发 - 沈大海
Android应用开发 - 沈大海
 
I os 16
I os 16I os 16
I os 16
 
Flex 4.5 action custom component development
Flex 4.5 action custom component developmentFlex 4.5 action custom component development
Flex 4.5 action custom component development
 
行動商務實務 - PhoneGap Advance
行動商務實務 - PhoneGap Advance行動商務實務 - PhoneGap Advance
行動商務實務 - PhoneGap Advance
 
Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天
Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天
Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天
 
I os 07
I os 07I os 07
I os 07
 
透過 Windows Azure Mobile Services 開發各平台 Apps
透過 Windows Azure Mobile Services 開發各平台 Apps透過 Windows Azure Mobile Services 開發各平台 Apps
透過 Windows Azure Mobile Services 開發各平台 Apps
 
利用Signalr打造即時通訊@Tech day geek
利用Signalr打造即時通訊@Tech day geek利用Signalr打造即時通訊@Tech day geek
利用Signalr打造即時通訊@Tech day geek
 
Unity遊戲設計- 應用Sprite物件
Unity遊戲設計- 應用Sprite物件Unity遊戲設計- 應用Sprite物件
Unity遊戲設計- 應用Sprite物件
 
Javascript之昨是今非
Javascript之昨是今非Javascript之昨是今非
Javascript之昨是今非
 
基于Eclipse和hadoop平台应用开发入门手册
基于Eclipse和hadoop平台应用开发入门手册基于Eclipse和hadoop平台应用开发入门手册
基于Eclipse和hadoop平台应用开发入门手册
 
Unity遊戲程式設計 - 應用Sprite物件
Unity遊戲程式設計 - 應用Sprite物件Unity遊戲程式設計 - 應用Sprite物件
Unity遊戲程式設計 - 應用Sprite物件
 
twMVC#23 | 快速上手 Azure Functions
twMVC#23 | 快速上手 Azure FunctionstwMVC#23 | 快速上手 Azure Functions
twMVC#23 | 快速上手 Azure Functions
 
行動平台上利用Facebook API開發社群應用程式
行動平台上利用Facebook API開發社群應用程式行動平台上利用Facebook API開發社群應用程式
行動平台上利用Facebook API開發社群應用程式
 
BizTalk練習投影片
BizTalk練習投影片BizTalk練習投影片
BizTalk練習投影片
 
[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式
[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式
[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式
 
Spring 2.x 中文
Spring 2.x 中文Spring 2.x 中文
Spring 2.x 中文
 
张所勇:前端开发工具推荐
张所勇:前端开发工具推荐张所勇:前端开发工具推荐
张所勇:前端开发工具推荐
 
Er introduction
Er introductionEr introduction
Er introduction
 

Plus de Jollen Chen

讓 HTML5 走進 IPTV Framework
讓 HTML5 走進 IPTV Framework讓 HTML5 走進 IPTV Framework
讓 HTML5 走進 IPTV Framework
Jollen Chen
 
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(3)
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(3)課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(3)
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(3)
Jollen Chen
 

Plus de Jollen Chen (18)

Flowchain blockchain classroom at Taiwan Tech University
Flowchain blockchain classroom at Taiwan Tech UniversityFlowchain blockchain classroom at Taiwan Tech University
Flowchain blockchain classroom at Taiwan Tech University
 
Bitmark and Hyperledger Workshop: the Digital Assets and Property
Bitmark and Hyperledger Workshop: the Digital Assets and PropertyBitmark and Hyperledger Workshop: the Digital Assets and Property
Bitmark and Hyperledger Workshop: the Digital Assets and Property
 
Introducing the Blockchain and Distributed Ledger Technology
Introducing the Blockchain and  Distributed Ledger TechnologyIntroducing the Blockchain and  Distributed Ledger Technology
Introducing the Blockchain and Distributed Ledger Technology
 
Maker of Things - the open IoT cloud for makers chapter.
Maker of Things - the open IoT cloud for makers chapter.Maker of Things - the open IoT cloud for makers chapter.
Maker of Things - the open IoT cloud for makers chapter.
 
WoT.City and IoT Protocols Movement @ Taipei, Taiwan
WoT.City and IoT Protocols Movement @ Taipei, TaiwanWoT.City and IoT Protocols Movement @ Taipei, Taiwan
WoT.City and IoT Protocols Movement @ Taipei, Taiwan
 
IoT and Maker Crossover (IMCO) Conference 2015
IoT and Maker Crossover (IMCO) Conference 2015IoT and Maker Crossover (IMCO) Conference 2015
IoT and Maker Crossover (IMCO) Conference 2015
 
Open IoT Cloud Architecture, Web of Things, Shenzhen, China.
Open IoT Cloud Architecture, Web of Things, Shenzhen, China.Open IoT Cloud Architecture, Web of Things, Shenzhen, China.
Open IoT Cloud Architecture, Web of Things, Shenzhen, China.
 
Backbone.js and MVW 101
Backbone.js and MVW 101Backbone.js and MVW 101
Backbone.js and MVW 101
 
Single-Page Application Design Principles 101
Single-Page Application Design Principles 101Single-Page Application Design Principles 101
Single-Page Application Design Principles 101
 
MongoDB & NoSQL 101
 MongoDB & NoSQL 101 MongoDB & NoSQL 101
MongoDB & NoSQL 101
 
Mokoversity Course: Apple Swift 101 - Introduction
Mokoversity Course: Apple Swift 101 - IntroductionMokoversity Course: Apple Swift 101 - Introduction
Mokoversity Course: Apple Swift 101 - Introduction
 
讓 HTML5 走進 IPTV Framework
讓 HTML5 走進 IPTV Framework讓 HTML5 走進 IPTV Framework
讓 HTML5 走進 IPTV Framework
 
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(3)
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(3)課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(3)
課程名稱:八屏一雲時代來臨 教你HTML5六小時打通(3)
 
Android HAL Introduction: libhardware and its legacy
Android HAL Introduction: libhardware and its legacyAndroid HAL Introduction: libhardware and its legacy
Android HAL Introduction: libhardware and its legacy
 
Jollen's Presentation: Introducing Android low-level
Jollen's Presentation: Introducing Android low-levelJollen's Presentation: Introducing Android low-level
Jollen's Presentation: Introducing Android low-level
 
Embedded Linux: Introduction
Embedded Linux: IntroductionEmbedded Linux: Introduction
Embedded Linux: Introduction
 
Android Application: Introduction
Android Application: IntroductionAndroid Application: Introduction
Android Application: Introduction
 
Android OS Porting: Introduction
Android OS Porting: IntroductionAndroid OS Porting: Introduction
Android OS Porting: Introduction
 

Android Wear SDK: Level 101

  • 1. Android Wear SDK 101 ⼀一⼩小時上⼿手 jollen <jollen@jollen.org> www.mokoversity.com 創新創業分享與交流 就在 Mokoversity Mokoversity 114年5月15⽇日星期四
  • 2. • Android SDK • ADT plugin for Eclipse v22.6.1+ • Install ‘Android Wear ARM EABI v7a System Image’ • Use the latest version of ‘Android Support Library’ • Set Up the Android Wear Emulator 開發環境 214年5月15⽇日星期四
  • 3. • Android SDK • Intent • Broadcast Receiver • Notification • Android Design guidelines for notifications • http://developer.android.com/guide/topics/ui/ notifiers/notifications.html 背景知識 314年5月15⽇日星期四
  • 4. 安裝 Android Wear SDK 414年5月15⽇日星期四
  • 5. 建⽴立 Android Wear Emulator 514年5月15⽇日星期四
  • 6. 1. Install and Use Genymotion 2. Setup ‘Nexus 5’ AVD 3. Start Android Wear emulator and Nexus 5 4. Install Android Wear Preview App on Nexus 5 5. Setup TCP port forwarding for ADB 6. Install and Open ‘Android Wear Preview’ on Nexus 5 建⽴立 Android Wear Emulator 614年5月15⽇日星期四
  • 7. $ ./adb -s [device name Nexus 5] install ./Android_Wear_Beta.apk $ ./adb -s [device name Nexus 5] forward tcp:5601 tcp:5601 Install Android Wear Preview App 需先下載 APK 套件 714年5月15⽇日星期四
  • 10. Install Android Wear Preview SDK 1014年5月15⽇日星期四
  • 11. import android.preview.support.wearable.notifications.*; import android.preview.support.v4.app.NotificationManagerCompat; import android.support.v4.app.NotificationCompat; Import Libraries 1114年5月15⽇日星期四
  • 13. •Designing for Android Wear is substantially different than designing for phones or tablets. •Android phones 與 tablets 是 UI、functionality 與 Touch 導向 •Android wear 是 Notification 導向 Android Wear App 的設計 1314年5月15⽇日星期四
  • 14. The Context Stream On the Android wearable, each notification appears as a new card in the context stream. The Cue Card Allows users to speak to their device. There are a list of actions. The list of actions includes Android intents for voice actions. UI Principles Portions of this page are reproduced from work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License. 1414年5月15⽇日星期四
  • 15. Step zero 從 Notification 開始 1514年5月15⽇日星期四
  • 16. private void zero() { NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setContentTitle("Zero") .setContentText("You're at step zero.") .setSmallIcon(R.drawable.bg_eliza); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(0x07, builder.build()); } Portions of this page are reproduced from work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License. Step Zero 1614年5月15⽇日星期四
  • 19. private void zeroX() { NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setContentTitle("ZeroX") .setContentText("You're at step zero.") .setSmallIcon(R.drawable.bg_eliza) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bg_eliza)); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(0x08, builder.build()); } Image 使⽤用 Large Icon for Android Wear 1914年5月15⽇日星期四
  • 22. private void one() { // Build an intent for an action to make a phone call Intent phoneCallIntent = new Intent(Intent.ACTION_VIEW); Uri phoneUri = Uri.parse("tel:119"); phoneCallIntent.setData(phoneUri); PendingIntent phoneCallPendingIntent = PendingIntent.getActivity(this, 0, phoneCallIntent, 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setContentTitle("One") .setContentText("You're at step one.") .setSmallIcon(R.drawable.bg_eliza) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bg_eliza)) .addAction(R.drawable.ic_full_reply, "Call me", phoneCallPendingIntent); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(0x09, builder.build()); } Step One 2214年5月15⽇日星期四
  • 25. • NotificationCompat.Builder Basic Android APIs 2514年5月15⽇日星期四
  • 26. •Android wearables provide just the right information at just the right time, allowing you to be connected to the virtual world and present in the real world. •Android Devices (Phone/Table) 的週邊 •Android Wear 是 Notification 裝置 •Android Devices 的 Second Device •PDA (輔具) Android Wear 的哲學 2614年5月15⽇日星期四
  • 27. •Contextually aware and smart •Glanceable •Zero/low interaction •Helpful Design Principles 2714年5月15⽇日星期四
  • 28. First app Add New Features for Wearables 2814年5月15⽇日星期四
  • 29. Actions Images Application Icons Pages Notification Stacks Voice Replies Notification UI Patterns 2914年5月15⽇日星期四
  • 30. private void appOne() { NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setContentTitle("First App") .setContentText("My first app for Android wear.") .setSmallIcon(R.drawable.bg_eliza) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bg_eliza)); Notification notification = new WearableNotifications.Builder(builder) .setMinPriority() .build(); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(0x01, notification); } Use Features for Wearables 3014年5月15⽇日星期四
  • 31. private void appOneX() { NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setContentTitle("Coupon") .setContentText("Limited. 30 mins from now !") .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bg_mountain)); Intent intent = new Intent(ACTION_RESPONSE); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT); builder.setContentIntent(pendingIntent); Notification notification = new WearableNotifications.Builder(builder) .setMinPriority() .build(); NotificationManagerCompat.from(this).notify(0x02, notification); } .setHintHideIcon(true) Use .setContentIntent() 3114年5月15⽇日星期四
  • 32. Skin of Android Wear 3214年5月15⽇日星期四
  • 34. // Broadcast receiver mFilterWearableReply = new IntentFilter(ACTION_RESPONSE); mWearableReceiver = new BroadcastReceiver() { @Override public void onReceive(Context arg0, Intent arg1) { Toast.makeText(mContext, "Someone wants to get a coupon.", Toast.LENGTH_LONG).show(); } }; registerReceiver(mWearableReceiver, mFilterWearableReply); 使⽤用 Broadcast Receiver 3414年5月15⽇日星期四
  • 35. Second & third app Add Pages to a Notification Voice Replies (Remote Input) 3514年5月15⽇日星期四
  • 37. private void appTwo() { NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setContentTitle("80% Discount") .setContentText("Now products are there...") .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bg_mountain)); Intent intent = new Intent(ACTION_RESPONSE); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT); builder.setContentIntent(pendingIntent); // 1. Create a big text style for the second page BigTextStyle secondPageStyle = new NotificationCompat.BigTextStyle(); secondPageStyle.setBigContentTitle("New Product") .bigText("A lot of text..."); // 2. Create second page notification with big text style Notification secondPageNotification = new NotificationCompat.Builder(this) .setStyle(secondPageStyle) .build(); Notification notification = new WearableNotifications.Builder(builder) .setMinPriority() .setHintHideIcon(true) // 3. Add the second page to notification .addPage(secondPageNotification) .build(); NotificationManagerCompat.from(this).notify(0x01, notification); } 3714年5月15⽇日星期四
  • 38. private void appThree() { NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setContentTitle("Coupon") .setContentText("Limited. 30 mins from now !") .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bg_mountain)); Intent intent = new Intent(ACTION_RESPONSE); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT); builder.setContentIntent(pendingIntent); Notification notification = new WearableNotifications.Builder(builder) .setMinPriority() .setHintHideIcon(true) .addRemoteInputForContentIntent( new RemoteInput.Builder("GET !") .setLabel("Reply").build()) .build(); NotificationManagerCompat.from(this).notify(0x02, notification); } 3814年5月15⽇日星期四
  • 39. Notification notification = new WearableNotifications.Builder(builder) .setMinPriority() .setHintHideIcon(true) .addRemoteInputForContentIntent( new RemoteInput.Builder("GET !") .setLabel("Reply") .setChoices(getResources().getStringArray(R.array.reply_choices)) .build()) .build(); Use .setChoices() 3914年5月15⽇日星期四
  • 41. 什麼是 Intent (意圖) 在 Android 應⽤用程式框架中,有⼀一個⾮非常聰明的事件處理 機制,稱之為「 Intent 」 在「 Intent 」這樣的事件處理觀念裡,Android 試圖將事 件解釋為「應⽤用程式的意圖」或是「使⽤用者的意圖」,並試 著去解釋該意圖的⺫⽬目的,若 Android 系統本⾝身能理解應⽤用 程式的意圖,便會「⾃自⾏行」去處理該意圖所應執⾏行的⼯工作 Android 的做法是,讓每個意圖(Intent)都帶有⼀一個動 作(action),並根據不同的動作去⾏行動 事件處理 4114年5月15⽇日星期四
  • 42. Intent 的類型 Intent 的動作 ⾃自⾏行定義 框架內部定義 Android 框架的 Intent 有很多⽅方便實⽤用的「內建動作」 Intent 定義 4214年5月15⽇日星期四
  • 43. Android內建的Intent Action Android 的框架確實是讓每個 Intent 都包含了⼀一個動作, 就稱為 action 範例: HelloIntentDialer: 啟動撥號器 (dialer) 並撥號 HelloIntentMusic : 使⽤用者按下「 Select Music 」 後,可以由⾳音樂清單裡選擇⾳音樂並撥放 HelloIntentWallpaper : 啟動 Android 內建的「背景 圖選擇器」,讓使⽤用者更換背景 Intent Action 4314年5月15⽇日星期四
  • 44. Intent 可包含 Data 除了 action 外, Intent 還可以包含另外⼀一項資訊 「data」 Intent 的 action 指定這個 Intent 的「動作」是什麼, 框架會依指定的動作進⾏行處理;有些 action 可以附帶⼀一筆 資料,這個資料是以 Uri 的格式撰寫 Intent Data 4414年5月15⽇日星期四
  • 45. 內建 Intent 範例 內建的 Intent 有哪些呢?請參考 Android Reference Guide 中的 Intent 類別說明 上述三個範例分別使⽤用以下三個 action: 1. ACTION_CALL: 撥號 2. ACTION_GET_CONTENT: 啟動內容選取器 3. ACTION_SET_WALLPAPER: 設定Wallpaper Intent 範例 4514年5月15⽇日星期四
  • 46. Intent Action 的類型 Android 內建的 Intent action 分為⼆二種: 1. Activity Action : 啟動 Activity 的 action 2. Broadcast Action : 透過廣撥器處理的action 第⼀一種 action 是 activity action ,⽤用途是通知框架啟 動 Activity ,這裡提出的三個範例,都是使⽤用 activity action Intent 的 Action 類型 4614年5月15⽇日星期四
  • 47. ⾃自動撥號: HelloIntentDialer public class HelloIntentDialer extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);   Intent dial = new Intent(); dial.setAction("android.intent.action.CALL"); dial.setData(Uri.parse("tel:119")); startActivity(dial); } } 「送出⼀一個帶有內建 action 的 Intent 給框架,因為 action 為 CALL,所以框架 會去啟動撥號 activity 並打電話。」 打電話範例 4714年5月15⽇日星期四
  • 48. 程式說明 1. 先產⽣生⼀一個 Intent 物件: Intent dial = new Intent(); 2. 設定 Intent 的 action 為 「 android.intent.action.CALL 」,這是⼀一個內建的 action: dial.setAction("android.intent.action.CALL"); 3. 內建 action「CALL」需要附帶⼀一筆資料,⽽而資料的寫 法是使⽤用 URI 格式: dial.setData(Uri.parse("tel:119")); 打電話範例 4814年5月15⽇日星期四
  • 49. 送出 Intent 4. 「CALL」是⼀一個 activity action,所以呼叫 startActivity() 向 Intent 送給框架: startActivity(dial); 打電話範例 4914年5月15⽇日星期四
  • 51. Notification, Intent & Broadcast Receiver WearableNotificationCompat UI Patterns 結論 5114年5月15⽇日星期四