Android編程開發(fā)之NotiFication用法詳解
本文實(shí)例講述了Android編程開發(fā)之NotiFication用法。分享給大家供大家參考,具體如下:
notification就是通知的意思,安卓中指通知欄,一般用在電話,短信,郵件,鬧鐘鈴聲,在手機(jī)的狀態(tài)欄上就會(huì)出現(xiàn)一個(gè)小圖標(biāo),提示用戶處理這個(gè)快訊,這時(shí)手從上方滑動(dòng)狀態(tài)欄就可以展開并處理這個(gè)快訊。
在幫助文檔中,是這么說的, notification類表示一個(gè)持久的通知,將提交給用戶使用NotificationManager。已添加的Notification.Builder,使其更容易構(gòu)建通知。
notification是一種讓你的應(yīng)用程序在沒有開啟情況下或在后臺(tái)運(yùn)行警示用戶。它是看不見的程序組件(Broadcast Receiver,Service和不活躍的Activity)警示用戶有需要注意的事件發(fā)生的最好途徑。
先來區(qū)分以下狀態(tài)欄和狀態(tài)條的區(qū)別:
1、狀態(tài)條就是手機(jī)屏幕最上方的一個(gè)條形狀的區(qū)域;
在狀態(tài)條有好多信息量:比如usb連接圖標(biāo),手機(jī)信號(hào)圖標(biāo),電池電量圖標(biāo),時(shí)間圖標(biāo)等等;
2、狀態(tài)欄就是手從狀態(tài)條滑下來的可以伸縮的view;
在狀態(tài)欄中一般有兩類(使用FLAG_標(biāo)記):
(1)正在進(jìn)行的程序; (2)是通知事件;
一個(gè)Notification傳送的信息有:
1、一個(gè)狀態(tài)條圖標(biāo);
2、在拉伸的狀態(tài)欄窗口中顯示帶有大標(biāo)題,小標(biāo)題,圖標(biāo)的信息,并且有處理該點(diǎn)擊事件:比如調(diào)用該程序的入口類;
3、閃光,LED,或者震動(dòng);
下面對(duì)Notification類中的一些常量,字段,方法簡(jiǎn)單介紹一下:
常量:
DEFAULT_ALL 使用所有默認(rèn)值,比如聲音,震動(dòng),閃屏等等
DEFAULT_LIGHTS 使用默認(rèn)閃光提示
DEFAULT_SOUNDS 使用默認(rèn)提示聲音
DEFAULT_VIBRATE 使用默認(rèn)手機(jī)震動(dòng)
注意:在加入手機(jī)震動(dòng)效果時(shí)一定要在項(xiàng)目清單文件中加入手機(jī)震動(dòng)權(quán)限:
下面通過簡(jiǎn)單額小實(shí)例來具體實(shí)現(xiàn)notification功能:
MainActivity.java:
package com.example.lession16_notifi; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.res.Resources.NotFoundException; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { private NotificationManager notificationManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 第一步:通過getSystemService()方法得到NotificationManager對(duì)象; notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } // 測(cè)試 public void test1(View v) { showNotification("來短信了", "5557", "hello!", R.drawable.ic_launcher, R.drawable.ic_launcher); } // 第二步:對(duì)Notification的一些屬性進(jìn)行設(shè)置比如:內(nèi)容,圖標(biāo),標(biāo)題,相應(yīng)notification的動(dòng)作進(jìn)行處理等等; public void showNotification(String tickerText, String contentTitle, String contentText, int iconId, int notiId) { // 創(chuàng)建一個(gè)Notification Notification notification = new Notification(); // 設(shè)置通知 消息 圖標(biāo) notification.icon = iconId; // 設(shè)置發(fā)出消息的內(nèi)容 notification.tickerText = tickerText; // 設(shè)置發(fā)出通知的時(shí)間 notification.when = System.currentTimeMillis(); // 設(shè)置顯示通知時(shí)的默認(rèn)的發(fā)聲、振動(dòng)、Light效果 notification.defaults = Notification.DEFAULT_VIBRATE;// 振動(dòng) // Notification notification = new Notification(R.drawable.ic_launcher,"有新的消息", System.currentTimeMillis()); // 3步:PendingIntent android系統(tǒng)負(fù)責(zé)維護(hù) PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,getIntent(), 0); // 4步:設(shè)置更加詳細(xì)的信息 notification.setLatestEventInfo(this, contentTitle, contentText,pendingIntent); // 5步:使用notificationManager對(duì)象的notify方法 顯示Notification消息 需要制定 // Notification的標(biāo)識(shí) notificationManager.notify(notiId, notification); } // 6步:使用notificationManager對(duì)象的cancelAll()方法取消 public void clearNoti(View v) { notificationManager.cancelAll();// 清除所有 } }
布局文件activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="22dp" android:onClick="test1" android:text="@string/text_notifi" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="60dp" android:onClick="clearNoti" android:text="@string/text_clear" /> </RelativeLayout>
布局效果:
切記實(shí)現(xiàn)震動(dòng)效果在清單文件中加入權(quán)限:
實(shí)現(xiàn)效果如下:
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android中GPS坐標(biāo)轉(zhuǎn)換為高德地圖坐標(biāo)詳解
最近因?yàn)楣拘枨?,在做GPS定位,并且將獲得的坐標(biāo)顯示在高德地圖上,但是實(shí)際效果跟我們期望的是有偏差的。通過查閱資料,才知道有地球坐標(biāo)、火星坐標(biāo)之說。下面這篇文章就詳細(xì)介紹了Android中GPS坐標(biāo)轉(zhuǎn)換為高德地圖坐標(biāo)的方法,需要的朋友可以參考下。2017-01-01Android實(shí)現(xiàn)九宮格手勢(shì)解鎖
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)九宮格手勢(shì)解鎖的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android無需申請(qǐng)權(quán)限撥打電話的兩種方式
android 打電話有兩種實(shí)現(xiàn)方式,第一種方法撥打電話跳轉(zhuǎn)到撥號(hào)界面,第二種方法,撥打電話直接進(jìn)行撥打,下面逐一給大家介紹這兩種方式,需要的朋友參考下吧2016-12-12android讀取Assets圖片資源保存到SD卡實(shí)例
本文為大家詳細(xì)介紹下android讀取Assets圖片資源保存到SD卡的具體實(shí)現(xiàn),感興趣的各位可以參考下哈,希望對(duì)大家有所幫助2013-07-07Android實(shí)現(xiàn)雅虎新聞?wù)虞d視差動(dòng)畫效果
這篇文章主要介紹了Android實(shí)現(xiàn)雅虎新聞?wù)虞d視差動(dòng)畫效果,通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08Android中Fragment的加載方式與數(shù)據(jù)通信詳解
本文主要介紹了Android中Fragment的加載方式與數(shù)據(jù)通信的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03Android實(shí)現(xiàn)View滑動(dòng)的6種方式
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)View滑動(dòng)的6種方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05淺談Android單元測(cè)試的作用以及簡(jiǎn)單示例
本篇文章主要介紹了淺談Android單元測(cè)試的作用以及簡(jiǎn)單示例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08