Android Wear 開發入門——如何建立一個手機與穿戴式裝置關聯的通知(Notification),androidwear

來源:互聯網
上載者:User

Android Wear 開發入門——如何建立一個手機與穿戴式裝置關聯的通知(Notification),androidwear

建立通知

為了建立在手機與穿戴式裝置中都能展現的通知,可以使用 NotificationCompat.Builder。通過該類建立的通知,系統會處理該通知是否展現在手機或者穿戴裝置中。

 

匯入必要的類庫

在開發之前首先需要匯入以下類庫

importandroid.support.v4.app.NotificationCompat;importandroid.support.v4.app.NotificationManagerCompat;importandroid.support.v4.app.NotificationCompat.WearableExtender;


 

使用Notification Builder建立通知

 v4 support library 庫允許開發人員使用最新的通知特性,如action button或者large icons,編譯環境需要在4以上。

使用支援庫開發的通知,需要建立NotificationCompat.Builder執行個體,發布通知使用notify(),如下代碼所示:

int notificationId = 001;// Build intent for notification contentIntent viewIntent = new Intent(this,ViewEventActivity.class);viewIntent.putExtra(EXTRA_EVENT_ID, eventId);PendingIntent viewPendingIntent =        PendingIntent.getActivity(this,0, viewIntent,0);NotificationCompat.BuildernotificationBuilder=        new NotificationCompat.Builder(this)        .setSmallIcon(R.drawable.ic_event)        .setContentTitle(eventTitle)        .setContentText(eventLocation)        .setContentIntent(viewPendingIntent);// Get an instance of the NotificationManager serviceNotificationManagerCompat notificationManager =        NotificationManagerCompat.from(this);// Build the notification and issues it with notification manager.notificationManager.notify(notificationId,notificationBuilder.build());


當這條通知出現在手機上是,使用者可以指定PendingIntent通過使用setContentIntent()方法觸發這條通知,當通知出現在手機中時,使用者何以向左滑動來銷毀出現的通知。

 

 

添加Action Button

除了定義的setcontentintent()主要內容的行為,你可以通過傳遞一個PendingIntent到addaction()方法添加其他行動。

例如,下面的代碼展示了與上面代碼相同類型的通知,但是添加了一個動作在view的上面。

// Build an intent for an action to view a mapIntent mapIntent = new Intent(Intent.ACTION_VIEW);Uri geoUri = Uri.parse("geo:0,0?q=" + Uri.encode(location));mapIntent.setData(geoUri);PendingIntent mapPendingIntent =        PendingIntent.getActivity(this, 0, mapIntent, 0);NotificationCompat.Builder notificationBuilder =        new NotificationCompat.Builder(this)        .setSmallIcon(R.drawable.ic_event)        .setContentTitle(eventTitle)        .setContentText(eventLocation)        .setContentIntent(viewPendingIntent)        .addAction(R.drawable.ic_map,                getString(R.string.map), mapPendingIntent);


在手機上,該action是以button形式附加在notification一起,在穿戴裝置中,該action是以一個大按鈕形式出現的,當使用者單擊該按鈕,intent中的內容將在手機中被調起。

 

指定穿戴式裝置專屬的行為

如果希望穿戴式裝置中的行為有別於手機,使用WearableExtender.addAction().當使用該方法,穿戴裝置中將不再展現使用NotificationCompat.Builder.addAction().的行為,也就是只顯示其在穿戴式裝置中。

// Create an intent for the reply actionIntent actionIntent = new Intent(this, ActionActivity.class);PendingIntent actionPendingIntent =        PendingIntent.getActivity(this, 0, actionIntent,                PendingIntent.FLAG_UPDATE_CURRENT);// Create the actionNotificationCompat.Action action =        new NotificationCompat.Action.Builder(R.drawable.ic_action,                getString(R.string.label, actionPendingIntent))                .build();// Build the notification and add the action via WearableExtenderNotification notification =        new NotificationCompat.Builder(mContext)                .setSmallIcon(R.drawable.ic_message)                .setContentTitle(getString(R.string.title))                .setContentText(getString(R.string.content))                .extend(new WearableExtender().addAction(action))                .build();


添加Big View

開發人員可以插入一個Big View形式的內容進入通知,在手機中,使用者可以通過展開通知來查看該Big View,在穿戴式裝置中,該Big View是預設可見的。

添加這種可展開的內容作為通知,使用 NotificationCompat.Builder 對象中的setStyle() 方法,傳遞給執行個體BigTextStyle 或者 InboxStyle樣式。

例如下面代碼是添加了BigTextStyle 到通知中。

// Specify the 'big view' content to display the long// event description that may not fit the normal content text.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();bigStyle.bigText(eventDescription);NotificationCompat.Builder notificationBuilder =        new NotificationCompat.Builder(this)        .setSmallIcon(R.drawable.ic_event)        .setLargeIcon(BitmapFractory.decodeResource(                getResources(), R.drawable.notif_background))        .setContentTitle(eventTitle)        .setContentText(eventLocation)        .setContentIntent(viewPendingIntent)        .addAction(R.drawable.ic_map,                getString(R.string.map), mapPendingIntent)        .setStyle(bigStyle);


為通知添加穿戴式裝置特性

如果想添加穿戴式裝置中特有的特性,如語音輸入等,可以使用NotificationCompat.WearableExtender 類,使用該類分以下幾步。

1 建立WearableExtender執行個體,設定穿戴式裝置特有的特性

2 建立NotificationCompat.Builder執行個體,設定如前文所述

3 調用extend()方法,傳遞給WearableExtender

4 調用build()建立通知

下面代碼使用 setHintHideIcon()移除通知卡片中的表徵圖

// Create a WearableExtender to add functionality for wearablesNotificationCompat.WearableExtender wearableExtender =        new NotificationCompat.WearableExtender()        .setHintHideIcon(true);// Create a NotificationCompat.Builder to build a standard notification// then extend it with the WearableExtenderNotification notif = new NotificationCompat.Builder(mContext)        .setContentTitle("New mail from " + sender)        .setContentText(subject)        .setSmallIcon(R.drawable.new_mail);        .extend(wearableExtender)        .build();


如果想在後面讀取特有的特性的值,可以如下代碼所示

NotificationCompat.WearableExtender wearableExtender =        new NotificationCompat.WearableExtender(notif);boolean hintHideIcon = wearableExtender.getHintHideIcon(); 

傳送通知

當需要傳遞通知時,使用 NotificationManagerCompat API代替NotificationManager:

// Get an instance of the NotificationManager serviceNotificationManagerCompat notificationManager =        NotificationManagerCompat.from(mContext);// Issue the notification with notification manager.notificationManager.notify(notificationId, notif);


當使用NotificationManager,一些 NotificationCompat.WearableExtender 的特性不能工作

NotificationCompat.WearableExtender wearableExtender =        new NotificationCompat.WearableExtender(notif);boolean hintHideIcon = wearableExtender.getHintHideIcon();



安卓開發中怎設定Notification的提醒時間

設定時間時記錄下Notification提醒的內容等資訊,當時間到時,發送Notification
PendingIntent notiPend = PendingIntent.getActivity(context, 0 , intent , PendingIntent.FLAG_CANCEL_CURRENT);
notification = new Notification(android.R.drawable.stat_notify_sync, notifyScroll, System.currentTimeMillis());
notification.setLatestEventInfo(context, notifyTitle, notifyContent, notiPend);
notifiMgr.notify(notifyId, notification);
 
問android系統中的進程,任務,服務三者的不同與聯絡?

進程是總稱,包括第三方應用和系統應用以及系統底層模組。任務是你安裝的第三方應用進程。而服務是系統模組進程。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.