自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android中經(jīng)過(guò)強(qiáng)化且準(zhǔn)備對(duì)接可穿戴設(shè)備的通知機(jī)制

譯文
移動(dòng)開發(fā) Android
在今天的文章中,我們將共同探討現(xiàn)代通知機(jī)制的實(shí)現(xiàn)方式,正如谷歌公司在今年的I/O大會(huì)上所展示。我們將利用新的支持軟件包并以其功能為基礎(chǔ)添加只能顯示在智能手表上的活動(dòng)信息——事實(shí)上,智能手表正是目前惟一正式面世了的Android Wear可穿戴設(shè)備。

【51CTO譯文】通知機(jī)制是一種非常實(shí)用的應(yīng)用程序用戶交互方式,而在Android Wear設(shè)備的支持下,我們現(xiàn)在已經(jīng)能夠利用可穿戴設(shè)備運(yùn)行Android系統(tǒng)并由此拓展出更為廣闊的適用范疇。有鑒于此,學(xué)習(xí)充分利用此類設(shè)備的功能特性就變得***價(jià)值,大家可以借此添加適當(dāng)?shù)幕顒?dòng)通知或者創(chuàng)建只顯示在可穿戴設(shè)備之上的通知信息。

在今天的文章中,我們將共同探討現(xiàn)代通知機(jī)制的實(shí)現(xiàn)方式,正如谷歌公司在今年的I/O大會(huì)上所展示。我們將利用新的支持軟件包并以其功能為基礎(chǔ)添加只能顯示在智能手表上的活動(dòng)信息——事實(shí)上,智能手表正是目前惟一正式面世了的Android Wear可穿戴設(shè)備。

1. 準(zhǔn)備工作

要完成今天的項(xiàng)目,大家可以使用Android Studio或者Android開發(fā)者工具。如果各位一直在使用Android Studio,那么需要確保將以下命令行添加到build.gradle文件當(dāng)中。

1

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

2. 建立新項(xiàng)目

后來(lái),大家首先啟動(dòng)自己的IDE并創(chuàng)建一個(gè)新的Android項(xiàng)目,當(dāng)然直接打開原先創(chuàng)建好的項(xiàng)目也是可行的。在今天的文章中,我們將創(chuàng)建一個(gè)新項(xiàng)目,并將其命名為ImprovedNotifications。另外,也別忘記為它選擇***的包名稱。

在創(chuàng)建項(xiàng)目的過(guò)程中,大家請(qǐng)確保在Create Activity步驟中選擇的是Empty Activity選項(xiàng)。

當(dāng)項(xiàng)目創(chuàng)建完成后,再創(chuàng)建一個(gè)新的Activity,也就是ActivatedActivity。該Activity將在后續(xù)運(yùn)行中被來(lái)自手機(jī)或者可穿戴設(shè)備的通知機(jī)制所調(diào)用。

在我們繼續(xù)下一步之前,需要將以下代碼添加到strings.xml文件中,文章后面需要對(duì)其加以使用。

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.     <string name="app_name">ImprovedNotifications</string> 
  4.     <string name="title_activity_activated">ActivatedActivity</string> 
  5.     
  6.     <string name="message">Hi"!" I"'"m the activated activity</string> 
  7.     <string name="button_text">Try me for a new notification</string> 
  8.     
  9.     <string name="notification_title">Hey Mom"!" I"'""m a title</string> 
  10.     <string name="notification_text">Look at me"!" I"'"m a sexy notification content</string> 
  11.     <string name="first_action">Let"'"s see the author"'"s twitter profile</string> 
  12.     
  13.     <string name="wearable_action">I only appear here</string> 
  14. </resources> 

3. 創(chuàng)建布局

完成上述工作后,現(xiàn)在要做的是為MainActivity以及ActivatedActivity類創(chuàng)建一套布局。其中MainActivity類的布局如以下代碼所示:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="http://schemas.android.com/tools" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     tools:context="${relativePackage}.${activityClass}" > 
  6.     <Button 
  7.         android:id="@+id/notification_button" 
  8.         android:layout_height="wrap_content" 
  9.         android:layout_width="wrap_content" 
  10.         android:layout_centerInParent="true" 
  11.         android:padding="10dp" 
  12.         android:text="@string/button_text"/> 
  13. </RelativeLayout> 

以下代碼則為ActivatedActivity類的布局。

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="http://schemas.android.com/tools" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     tools:context="${relativePackage}.${activityClass}" > 
  6.     <TextView 
  7.         android:layout_width="wrap_content" 
  8.         android:layout_height="wrap_content" 
  9.         android:padding="10dp" 
  10.         android:layout_centerInParent="true" 
  11.         android:text="@string/message"/> 
  12. </RelativeLayout> 

#p#

4. 創(chuàng)建一套通知機(jī)制

我們需要在MainActivity類當(dāng)中創(chuàng)建一套通知機(jī)制。在以下代碼片段中,大家可以看到要采取哪些步驟才能生成一條通知。我已經(jīng)在代碼當(dāng)中加入注釋,旨在幫助大家了解各個(gè)步驟的具體含義。不過(guò)別擔(dān)心,稍后我們會(huì)再次分步對(duì)其實(shí)現(xiàn)流程加以解讀。

  1. package com.androiheroes.improvednotifications; 
  2.   
  3. import android.app.Activity; 
  4. import android.app.Notification; 
  5. import android.app.PendingIntent; 
  6. import android.content.Intent; 
  7. import android.net.Uri; 
  8. import android.os.Bundle; 
  9. import android.support.v4.app.NotificationCompat; 
  10. import android.support.v4.app.NotificationManagerCompat; 
  11. import android.view.View; 
  12. import android.widget.Button; 
  13.   
  14. public class MainActivity extends Activity { 
  15.   
  16.     /* 我們將要使用的功能部件 */ 
  17.   private Button button; 
  18.    
  19.   /* 
  20.    * 這里是通知信息ID 
  21.    *大家可以利用它忽略在通知管理器上調(diào)用cancel()方法的通知信息。 
  22.    */ 
  23.   private int notification_id = 1
  24.   private final String NOTIFICATION_ID = "notification_id"
  25.    
  26.   /* 這些是我們用于啟動(dòng)通知機(jī)制的類。 */ 
  27.   private NotificationCompat.Builder notification_builder; 
  28.   private NotificationManagerCompat notification_manager; 
  29.    
  30.   @Override 
  31.   protected void onCreate(Bundle savedInstanceState) { 
  32.     super.onCreate(savedInstanceState); 
  33.     setContentView(R.layout.activity_main); 
  34.      
  35.     /* 
  36.      * Step 1 
  37.      * Instantiation of the button you use to start the notification 
  38.      */ 
  39.     button = (Button) findViewById(R.id.notification_button); 
  40.      
  41.     /* 
  42.      * 第二步 
  43.      *創(chuàng)建intent后,我們就可以保證用戶在點(diǎn)觸通知欄后將其啟動(dòng), 
  44.      *并由PendingIntent對(duì)其加以處理。 
  45.      */ 
  46.     Intent open_activity_intent = new Intent(this, ActivatedActivity.class); 
  47.     open_activity_intent.putExtra(NOTIFICATION_ID, notification_id); 
  48.     PendingIntent pending_intent = PendingIntent.getActivity(this0, open_activity_intent, PendingIntent.FLAG_CANCEL_CURRENT); 
  49.          
  50.     /* 
  51.      * 第三步 
  52.      *在這里,我們創(chuàng)建通知機(jī)制并向其中添加實(shí)際運(yùn)行中需要使用的全部屬性。 
  53.      */ 
  54.     notification_builder = new NotificationCompat.Builder(this
  55.       .setSmallIcon(R.drawable.ic_launcher) 
  56.       .setContentTitle(getString(R.string.notification_title)) 
  57.       .setContentText(getString(R.string.notification_text)) 
  58.       /* 
  59.        *這一方法要求我們的通知機(jī)制必須具備通知信息的全部默認(rèn)特征, 
  60.        * 例如聲音與振動(dòng)提示。 
  61.        */ 
  62.       .setDefaults(Notification.DEFAULT_ALL) 
  63.       /*這一方法將在用戶點(diǎn)觸通知欄后將其忽略。 */ 
  64.       .setAutoCancel(true
  65.       .setContentIntent(pending_intent); 
  66.      
  67.     /* 
  68.      * 第四步 
  69.      *在這里,我們對(duì)通知管理器對(duì)象進(jìn)行實(shí)例化,從而開始/停止通知機(jī)制。 
  70.      */ 
  71.     notification_manager = NotificationManagerCompat.from(this); 
  72.   } 
  73.    
  74.   @Override 
  75.   protected void onStart() { 
  76.     super.onStart(); 
  77.      
  78.     /* 
  79.      * 第五步 
  80.      * 當(dāng)我們點(diǎn)觸屏幕上的按鈕時(shí),通知內(nèi)容就會(huì)顯示出來(lái)。 
  81.      */ 
  82.     button.setOnClickListener(new View.OnClickListener() { 
  83.       @Override 
  84.       public void onClick(View v) { 
  85.         notification_manager.notify(notification_id, notification_builder.build()); 
  86.       } 
  87.     }); 
  88.   } 

***步

我們首先對(duì)用于啟動(dòng)通知機(jī)制的按鈕進(jìn)行實(shí)例化。大家也可以直接在onCreate方法中創(chuàng)建通知,但使用按鈕方式能夠讓我們更為確切地對(duì)通知時(shí)間加以控制。

第二步

在第二步中,我們需要在通知欄被點(diǎn)觸后對(duì)Intent對(duì)象進(jìn)行實(shí)例化并執(zhí)行對(duì)應(yīng)任務(wù)。我們將該對(duì)象傳遞至PendingIntent實(shí)例,并在后續(xù)調(diào)用時(shí)再進(jìn)行處理。

第三步

利用Android支持庫(kù),我們使用NotificationCompat對(duì)象中的Builder類創(chuàng)建通知,而后為其設(shè)定屬性。

第四步

在這一步中,我們對(duì)NotificationManagerCompat實(shí)例進(jìn)行實(shí)例化,從而在任意必要時(shí)間點(diǎn)開始并/或停止通知機(jī)制。這樣一來(lái),大家就能更輕松地對(duì)自己的成果進(jìn)行測(cè)試。

第五步

當(dāng)該按鈕被點(diǎn)觸時(shí),通知會(huì)利用notify方法加以啟動(dòng)。

當(dāng)然,不要忘記使用由Android支持庫(kù)提供的各種類。通過(guò)這種方式,大家能夠確保自己的通知機(jī)制在舊有Android版本上同樣擁有與其風(fēng)格相符的顯示效果。

現(xiàn)在大家可以運(yùn)行這款應(yīng)用,點(diǎn)觸按鈕并在屏幕上方查看到通知內(nèi)容了。如果大家點(diǎn)擊通知部分,則會(huì)切換至ActivatedActivity activity當(dāng)中。既然通知機(jī)制已經(jīng)創(chuàng)建完成并開始正常運(yùn)轉(zhuǎn),接下來(lái)要做的就是向其中添加活動(dòng)信息了。

5.向通知中添加活動(dòng)

大家可以通過(guò)在notification_builder對(duì)象上調(diào)用addAction方法來(lái)向通知中添加額外活動(dòng)。為了使其確切起效,我們需要向其傳遞一個(gè)PendingIntent實(shí)例,其中包含有即將執(zhí)行的對(duì)應(yīng)任務(wù)。

在以下代碼片段中,我向大家展示了如何分步創(chuàng)建出一項(xiàng)包含有定制任務(wù)的活動(dòng)。在該示例當(dāng)中,我將采用自己在Twitter應(yīng)用中的Twitter配置文件。也就是說(shuō),我需要利用一個(gè)URI實(shí)例指向自己的Twitter配置文件,將其添加到Intent當(dāng)中,而后在活動(dòng)被點(diǎn)觸時(shí)由PendingIntent對(duì)其進(jìn)行處理。將以下代碼添加到notification_builder對(duì)象的實(shí)例化部分之前。

  1. /*移動(dòng)通知機(jī)制中的活動(dòng)必須執(zhí)行某項(xiàng)任務(wù)。 
  2.  *在這里的示例中,用戶點(diǎn)觸后該作者的Twitter配置文件會(huì)在Twitter應(yīng)用中被打開。 
  3.  *但大家也可以根據(jù)實(shí)際需要在自己的配置文件中對(duì)操作方式加以修改。 
  4.  */ 
  5.  Intent open_twitter_profile = new Intent(Intent.ACTION_VIEW); 
  6.  Uri twitter_profile_location = Uri.parse("twitter://user?screen_name=@kerpie"); 
  7.  open_twitter_profile.setData(twitter_profile_location); 
  8.  PendingIntent twitter_intent = PendingIntent.getActivity(this0, open_twitter_profile, 0); 

為了添加該活動(dòng),我們需要在notification_builder對(duì)象上調(diào)用addAction方法并將其傳遞至我們剛剛創(chuàng)建完成的open_twitter_profile對(duì)象當(dāng)中。

  1. /* 
  2.  *大家在這里創(chuàng)建自己的通知機(jī)制, 
  3.  *并將所有需要使用的屬性添加進(jìn)來(lái)。 
  4.  */ 
  5.           
  6. notification_builder = new NotificationCompat.Builder(this
  7.     .setSmallIcon(R.drawable.ic_launcher) 
  8.     .setContentTitle(getString(R.string.notification_title)) 
  9.     .setContentText(getString(R.string.notification_text)) 
  10.     /* 
  11.      *這一方法要求我們的通知包含所有必要的默認(rèn)通知特征, 
  12.      * 例如聲音與振作提示機(jī)制。 
  13.      */         
  14.     .setDefaults(Notification.DEFAULT_ALL) 
  15.     /*這一方法將在被點(diǎn)觸時(shí)忽略當(dāng)前通知信息。 */ 
  16.     .setAutoCancel(true
  17.     .setContentIntent(pending_intent) 
  18.     /* 
  19.      *大家可以在這里向自己的移動(dòng)設(shè)備添加活動(dòng), 
  20.      *需要注意我們所添加的活動(dòng)數(shù)量。 
  21.      *在本示例以及大多數(shù)情況下,活動(dòng)數(shù)量越少越好。 
  22.      */ 
  23.     .addAction(android.R.drawable.ic_dialog_info, getString(R.string.first_action), twitter_intent); 

運(yùn)行該應(yīng)用程序,點(diǎn)觸按鈕以觸發(fā)通知機(jī)制,這時(shí)大家應(yīng)該會(huì)看到通知內(nèi)容會(huì)與我們剛剛創(chuàng)建的活動(dòng)一同顯示出來(lái)。

盡管我們完全可以利用addAction方法向同一條通知中添加更多活動(dòng),但必須確保用戶不會(huì)因?yàn)榛顒?dòng)數(shù)量太多而感到無(wú)所適從。

#p#

6. Android Wear實(shí)際支持效果

講到這里,我們已經(jīng)利用來(lái)自Android支持庫(kù)的各種類保證了通知內(nèi)容能夠切實(shí)顯示在運(yùn)行Android Wear系統(tǒng)的智能手表之上。大家可以在智能手表真機(jī)上運(yùn)行該應(yīng)用,也可以嘗試在Android虛擬設(shè)備管理器中提供的模擬器內(nèi)感受其實(shí)際效果。無(wú)論采取哪種方式,我們都需要將自己的移動(dòng)設(shè)備與智能手表進(jìn)行同步。

在將移動(dòng)設(shè)備與智能手表模擬器進(jìn)行同步之前,大家首先需要安裝Android Wear應(yīng)用程序,現(xiàn)在各位已經(jīng)能從Google Play上下載到這款應(yīng)用。在將所有其它與計(jì)算機(jī)相連接的Android設(shè)備拔下之后,在命令行中執(zhí)行以下命令。

  1. adb devices 

這條命令顯示出當(dāng)前已經(jīng)連入開發(fā)計(jì)算機(jī)的所有設(shè)備。大家應(yīng)該能夠從中看到兩條項(xiàng)目,分別為智能手表模擬器與自己的移動(dòng)設(shè)備。接下來(lái)在命令行中運(yùn)行以下命令以啟用端口轉(zhuǎn)發(fā)功能。

  1. adb -d forward tcp:5601 tcp:5601 

現(xiàn)在大家可以將移動(dòng)設(shè)備與智能手表模擬器相連,并利用Android Wear應(yīng)用程序啟動(dòng)通知機(jī)制了。再次運(yùn)行該應(yīng)用程序,然后觸發(fā)通知。通知內(nèi)容的顯示效果應(yīng)該如下圖所示。

7. 添加只能顯示在可穿戴設(shè)備上的活動(dòng)

我們也可以添加一些只會(huì)顯示在可穿戴設(shè)備上的活動(dòng)內(nèi)容。要實(shí)現(xiàn)這一目標(biāo),我們需要調(diào)用WearableExtender類中的addAction方法。這樣處理的結(jié)果是,任何被添加到NotificationCompat.Builder類中的活動(dòng)都會(huì)被直接忽略。

與之前的實(shí)現(xiàn)方式一樣,要觸發(fā)該活動(dòng),我們需要利用Intent與PendingIntent實(shí)例。但除此之外,我們還需要利用特殊Action類中的Builder類創(chuàng)建出顯示在可穿戴設(shè)備上的活動(dòng)——如下所示,這一Builder類為NotificationCompat類的組成部分。

  1. /*在這里,我們要對(duì)智能手表中的活動(dòng)被點(diǎn)觸時(shí)所需要使用的Intent進(jìn)行實(shí)例化。 */ 
  2. Intent wearable_intent = new Intent(this, ActivatedActivity.class); 
  3. PendingIntent wearable_pending_intent = PendingIntent.getActivity(this0, wearable_intent, PendingIntent.FLAG_UPDATE_CURRENT); 
  4.   
  5. /*現(xiàn)在我們已經(jīng)創(chuàng)建出werable intent,接下來(lái)我們還需要?jiǎng)?chuàng)建wearable action來(lái)對(duì)該intent加以使用*/ 
  6. NotificationCompat.Action wearable_action = new NotificationCompat.Action.Builder( 
  7.         android.R.drawable.ic_dialog_email, 
  8.         getString(R.string.wearable_action), 
  9.         wearable_pending_intent).build(); 

接下來(lái),我們?nèi)缦滤纠胑xtend方法將該活動(dòng)添加到notification_builder對(duì)象當(dāng)中。

  1. /* 
  2.  *我們?cè)谶@里創(chuàng)建通知, 
  3.  *并將所有需要使用的屬性添加至其中。 
  4.  */ 
  5.           
  6. notification_builder = new NotificationCompat.Builder(this
  7.     .setSmallIcon(R.drawable.ic_launcher) 
  8.     .setContentTitle(getString(R.string.notification_title)) 
  9.     .setContentText(getString(R.string.notification_text)) 
  10.     /* 
  11.      *這一方法要求我們的通知包含所有必要的默認(rèn)通知特征, 
  12.      *例如聲音與振作機(jī)制。 
  13.      */         
  14.     .setDefaults(Notification.DEFAULT_ALL) 
  15.     /*這一方法將在被點(diǎn)觸時(shí)忽略當(dāng)前通知信息。*/ 
  16.     .setAutoCancel(true
  17.     .setContentIntent(pending_intent) 
  18.     /* 
  19.      *大家可以在這里向自己的移動(dòng)設(shè)備添加活動(dòng), 
  20.      *需要注意我們所添加的活動(dòng)數(shù)量。 
  21.      *在本示例以及大多數(shù)情況下,活動(dòng)數(shù)量越少越好。 
  22.      */ 
  23.     .addAction(android.R.drawable.ic_dialog_info, getString(R.string.first_action), twitter_intent) 
  24.     /* 
  25.      *我們?cè)谶@里添加只適用于可穿戴設(shè)備的活動(dòng)。 
  26.      *該活動(dòng)不會(huì)被顯示在移動(dòng)設(shè)備之上。 
  27.      */ 
  28.     .extend(new WearableExtender().addAction(wearable_action)); 

運(yùn)行應(yīng)用程序,點(diǎn)觸該按鈕從而在自己的可穿戴設(shè)備上顯示通知內(nèi)容。實(shí)際運(yùn)行效果應(yīng)該與可穿戴模擬器上的彈出通知有所不同。

總結(jié)

智能手表可謂蓄勢(shì)待發(fā),至少在未來(lái)一段時(shí)間內(nèi)它將成為全世界的關(guān)注重點(diǎn),因此充分利用這種新型通信方式服務(wù)于應(yīng)用程序用戶就顯得至關(guān)重要。我希望大家能夠從今天的文章中找到亮點(diǎn)與收獲,也歡迎各位通過(guò)評(píng)論欄與我們分享您的建議與意見。

原文鏈接:Enhanced and Wearable-Ready Notifications on Android

核子可樂(lè)譯

責(zé)任編輯:閆佳明 來(lái)源: 51CTO譯文
相關(guān)推薦

2014-11-12 13:37:57

可穿戴設(shè)備英特爾

2015-01-16 16:39:51

2021-02-03 00:18:05

可穿戴設(shè)備醫(yī)療應(yīng)用物聯(lián)網(wǎng)

2018-11-16 09:00:05

可穿戴設(shè)備智能測(cè)試

2021-11-22 21:59:47

物聯(lián)網(wǎng)醫(yī)學(xué)可穿戴設(shè)備

2021-03-16 11:24:53

物聯(lián)網(wǎng)可穿戴設(shè)備IOT

2024-05-09 10:28:09

IIoT可穿戴設(shè)備工業(yè)物聯(lián)網(wǎng)

2015-06-16 17:15:39

無(wú)線技術(shù)可穿戴設(shè)備

2020-02-25 16:48:11

物聯(lián)網(wǎng)可穿戴設(shè)備智能眼鏡

2014-05-05 11:02:02

2014-04-24 09:42:06

樂(lè)跑手環(huán)運(yùn)動(dòng)手環(huán)健康手環(huán)

2014-07-03 14:38:50

2014-05-20 17:04:08

智能硬件可穿戴設(shè)備反面教材

2015-01-05 09:56:20

可穿戴設(shè)備

2013-08-23 10:45:30

可穿戴設(shè)備

2015-10-22 11:08:19

2016-07-11 14:24:52

Android可穿戴

2016-01-28 09:51:55

2013-11-04 13:15:59

Google

2014-02-17 09:15:20

MWC可穿戴設(shè)備
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)