Android開發(fā)入門之Notification用法分析
本文實(shí)例講述了Android中Notification用法。分享給大家供大家參考,具體如下:
Notification可以理解為通知的意思一般用來(lái)顯示廣播信息 用Notification就必須要用到NotificationManager
想用Notification一般有三個(gè)步驟,如下所示
① 一般獲得系統(tǒng)級(jí)的服務(wù)NotificationManager。
調(diào)用Context.getSystemService(NOTIFICATION_SERVICE)方法即可返回NotificationManager實(shí)例
② 實(shí)例化Notification,并設(shè)置其屬性
用Notification構(gòu)造函數(shù) public Notification(int icon, CharSequence tickerText, long when)構(gòu)造Notification實(shí)例
③ 通過(guò)NotificationManager發(fā)通知就OK了
NotificationManager有兩個(gè)方法:notify()發(fā)出通知 cancel(...)取消通知
下面通過(guò)一個(gè)代碼實(shí)例來(lái)介紹一下Notification
先初始化notificationManager和notification兩個(gè)成員變量
notificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE); notification = new Notification(R.drawable.touxiang,"信息",System.currentTimeMillis());
PS:Notification構(gòu)造函數(shù)里傳的參數(shù)就是這樣顯示的第一個(gè)參數(shù)是 圖像,第二個(gè)是標(biāo)題 :信息 ,第三個(gè)是系統(tǒng)時(shí)間 (見圖一) 之所以在這說(shuō)一下這個(gè)構(gòu)造函數(shù)是想與下面的setLatestEventInfo(...)作個(gè)區(qū)別。
( 圖一 )
( 圖二 )
布局就不貼了 是兩個(gè)Button 看看發(fā)送按鈕
sendButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(NotificationActivity.this,NotificationActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(NotificationActivity.this, 0, intent, 0); notification.setLatestEventInfo(NotificationActivity.this, "你的一條信息", "來(lái)自張三的信息", pendingIntent); notificationManager.notify(ID,notification); notificationManager.notify(ID+1, notification); } })
setLatestEventInfo(...)里面所傳的參數(shù)的效果圖:(見圖二)通知里面有兩條完全一樣的消息,是的 你沒看錯(cuò),這是因?yàn)槲矣胣otificationManager notify(通知)了兩次 而且ID不同,ID是int型是通知信息的標(biāo)示符。雖然我上面兩條信息是一模一樣的,但由于ID的不同 , 所以Android還是會(huì)顯示兩條信息。
在此說(shuō)一下參數(shù)pendingIntent的在setLatestEventInfo里所扮演的角色,是啥子意思呢?pendingIntent可以在另外的地方執(zhí)行,不是立即意圖。當(dāng)用戶點(diǎn)擊擴(kuò)展通知的時(shí)候 pendingIntent意圖才開始執(zhí)行,例如圖二 我點(diǎn)擊其中一個(gè)消息后,立馬就進(jìn)入另外一個(gè)Activity...
正如上面看到的那樣,除了為notification設(shè)置圖標(biāo),標(biāo)題外還可以設(shè)置提示音,震動(dòng),閃光燈 詳情請(qǐng)見我轉(zhuǎn)的一片文章Notification使用詳解.....
package com.study.android; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button startBtn; private Button cancelBtn; private static final int HELLO_ID = 1; NotificationManager mNotificationManager; Notification mNotification; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startBtn = (Button)findViewById(R.id.startBtn); cancelBtn = (Button)findViewById(R.id.cancelBtn); // ① 獲取NotificationManager的引用 String ns = Context.NOTIFICATION_SERVICE; mNotificationManager = (NotificationManager)this.getSystemService(ns); // ② 初始化Notification int icon = R.drawable.ic_launcher; CharSequence tickerText = "Hello"; long when = System.currentTimeMillis(); mNotification = new Notification(icon,tickerText,when); mNotification.defaults = Notification.DEFAULT_ALL; mNotification.flags |= Notification.FLAG_NO_CLEAR; mNotification.flags |= Notification.FLAG_SHOW_LIGHTS; // ③ 定義notification的消息 和 PendingIntent Context context = this; CharSequence contentTitle ="My notification"; CharSequence contentText = "Hello World"; Intent notificationIntent = new Intent(this,MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,0 ); mNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); // ④ 把封裝好的notification傳入NotificationManager // 開啟通知 startBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mNotificationManager.notify(HELLO_ID,mNotification); } }); // 取消通知 cancelBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mNotificationManager.cancel(HELLO_ID); } }); } }
代碼中有創(chuàng)建Notification步驟,詳情可以看一下。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android視圖View技巧總結(jié)》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進(jìn)階教程》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android開發(fā)四大組件之實(shí)現(xiàn)電話攔截和電話錄音
這篇文章給大家介紹Android開發(fā)四大組件之實(shí)現(xiàn)電話攔截和電話錄音,涉及到android四大基本組件在程序中的應(yīng)用,對(duì)android四大基本組件感興趣的朋友可以參考下本篇文章2015-10-10Android實(shí)現(xiàn)系統(tǒng)的桌面圖標(biāo)文字的雙行顯示效果
這篇文章主要介紹了Android實(shí)現(xiàn)系統(tǒng)的桌面圖標(biāo)文字的雙行顯示效果,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-10-10Android UTF-8轉(zhuǎn)碼實(shí)例詳解
這篇文章主要介紹了Android UTF-8轉(zhuǎn)碼實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03Android提高之SQLite分頁(yè)讀取實(shí)現(xiàn)方法
這篇文章主要介紹了Android的SQLite分頁(yè)讀取實(shí)現(xiàn)方法,在Android項(xiàng)目開發(fā)中非常實(shí)用,需要的朋友可以參考下2014-08-08Android中ViewFlipper和AdapterViewFlipper使用的方法實(shí)例
ViewFlipper和AdapterViewFlipper是Android自帶的一個(gè)多頁(yè)面管理控件,下面這篇文章主要給大家介紹了關(guān)于Android中ViewFlipper和AdapterViewFlipper使用的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05Android Studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器APP
這篇文章主要為大家詳細(xì)介紹了Android Studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器APP,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03