Android NOtification 使用(震動 閃屏 鈴聲)

來源:互聯網
上載者:User

標籤:android   style   http   io   ar   os   使用   java   sp   

 一、 Notification 簡介 
在 android 系統中,在應用程式可能會遇到幾種情況需要通知使用者,有的需要使用者回應,有的則不需要,例如: 
* 當儲存檔案等事件完成,應該會出現一個小的訊息,以確認儲存成功。 
* 如果應用程式在後台運行,需要使用者的注意,應用程式應該建立一個通知,允許使用者在他或她的回應提供便利 
* 如果應用程式正在執行的工作,使用者必須等待(如裝載檔案),應用程式應該顯示進度或等待提醒。 

針對這些情況, android 都提供了不同的提醒方式。主要包括下面幾種: 

1.Toast Notification 是指出現在螢幕上的暫時性通知,這種通知用於傳達一些告知類型的訊息,短暫停留後會自動消失,無需使用者互動。比如告知下載已完成等。 (Toast Noification 這個說法最早是源於一個前 MSN 員工的提法, 因為 MSN 的訊息提醒是從底部向上輕彈,形式上很像一個麵包從烤麵包機中彈起的樣子,所以稱之為 Toast Noification 。 ) 
2.Status Bar Notification 是指以一個表徵圖或者捲軸文本的形式出現在系統頂部狀態列上的通知。當應用程式處於後台運行狀態時,這種方式比較合適。這種通知形式的好處是既能即使被關注到,又無需打斷當前任務,可以從頂部下拉查看通知摘並做選擇性處理。 
3.Dialog Notification 類似於 iOS 的 Alert Notification ,以交談視窗的形式出現在螢幕上,用於重要或需及時處理的通知。 

下面我們先瞭解以下 Android notification 的整個架構。前二種提醒方式都是由 NotificationManagerService ,而 Dialog Notification ,則是彈出一個視窗形 式實現的,因為這種提醒方式大多是針對當前應用程式或進程,所以它只是一種簡單且直觀的表達方式。 



二、 Notification的使用 
1.Toast
Toast 是 Android 中用來顯示顯示資訊的一種機制,和 Dialog 不一樣的是, Toast 是沒有焦點的,而且 Toast 顯示的時間有限,過一定的時間就會自動消失 

// 使用 TOAST 方法顯示結果內容 
Toast textToast=Toast.makeText(this, " 提示內容 ", Toast.LENGTH_LONG); 
//... 這裡也可以對 Toast 添加一些屬性 
textToast.show(); 
2. StatusBar Notification 
StatusBar Notification 是在系統狀態列上 增加了一個狀態列表徵圖,並在“通知“視窗中顯示提示資訊。當使用者選擇展開郵件, Android 就會發送一個通知(通常是推出一個活動)定義的意向。您也可以配置通知,提醒和聲音,震動的使用者,並在裝置上閃爍的燈光。 
這樣的通知是很理想的工作時,您的應用程式在後台服務,需要通知有關事件的使用者。如果您需要提醒有關事件已經發生,而你的活動仍可以在當前焦點,此時可以考慮使用一個對話方塊通知代替。 

StatusBar Notification 基本步驟如下: 

1 )得到 NotificationManager : 
String ns = Context.NOTIFICATION_SERVICE; 
NotificationManager mNotificationManager = (NotificationManager) getSystemService( ns ); 

2 )建立一個新的 Notification 對象: 
Notification notification = new Notification(); 
notification.icon = R.drawable.notification_icon; 
// 也可以使用稍微複雜一些的方式建立 Notification : 
int icon = R.drawable.notification_icon; 通知表徵圖 
CharSequence tickerText = "Hello"; // 狀態列 (Status Bar) 顯示的通知文本提示 
long when = System.currentTimeMillis(); // 通知產生的時間,會在通知資訊裡顯示 
Notification notification = new Notification(icon, tickerText, when) ;

3 )填充 Notification 的各個屬性: 
Context context = getApplicationContext(); 
CharSequence contentTitle = "My notification"; 
CharSequence contentText = "Hello World!"; 
Intent notificationIntent = new Intent(this, MyClass.class); 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0); 
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 

Notification 提供了豐富的手機提示方式: 
a) 在狀態列 (Status Bar) 顯示的通知文本提示,如: 
notification.tickerText = "hello"; 

b) 發出提示音,如: 
notification.defaults |= Notification.DEFAULT_SOUND; 
notification.sound = Uri.parse("file:/ sdcard /notification/ringer.mp3"); 
notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6"); 

c) 手機震動,如: 
notification.defaults |= Notification.DEFAULT_VIBRATE; 
long[] vibrate = {0,100,200,300}; 
notification.vibrate = vibrate ;

d)LED 燈閃爍,如: 
notification.defaults |= Notification.DEFAULT_LIGHTS; 
notification.ledARGB = 0xff00ff00; 
notification.ledOnMS = 300; 
notification.ledOffMS = 1000; 
notification.flags |= Notification.FLAG_SHOW_LIGHTS; 

e) 添加 remote view 
通過 RemoteViews 設定 notification 中 View 的屬性 
notification.contentView = new RemoteViews(getApplication().getPackageName(), R.layout.custom_dialog); 
notification.contentView.setProgressBar(R.id.pb, 100, 0, false); 
notification.contentView.setTextViewText(R.id.tv, " 進度 " + _progress+ "%"); 

4 )發送通知: 
private static final int ID_NOTIFICATION = 1; 
mNotificationManager.notify(ID_NOTIFICATION, notification); 


3.Dialog Notification

3.1 AlertDialog 

為了建立一個警告對話方塊,使用 AlertDialog.Builder 子類。通過 AlertDialog.Builder 
(Context) 擷取一個構造器然後使用這個類的公用方法來定義警告對話方塊的所有屬性。當得到構造器後,通過 create(). 方法來擷取警告對話方塊對象。有時我是不調用 create() 的,而是在設定好了後直接調用 show() 顯示 AlertDialog 。 

AlertDialog.Builder builder=newAlertDialog.Builder(this); 
builder.setMessage("Areyousureyouwanttoexit?") ; 
AlertDialog alert=builder.create(); 

3.2 ProcessDialog 

ProgressDialog 是 AlertDialog 類的一個擴充,可以為一個未定義進度的任務顯示一個旋轉輪形狀的進度動畫,或者為一個指定進度的任務顯示一個進度條。 

ProgressDialog progressDialog=newProgressDialog(getApplicationContext()); 
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
progressDialog.setIcon(R.drawable.alert_dialog_icon); 
progressDialog.setMessage("Loading..."); 
progressDialog.setCancelable(false);

複製去Google翻譯翻譯結果 

Android NOtification 使用(震動 閃屏 鈴聲)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.