Android編程開發(fā)之NotiFication用法詳解
本文實例講述了Android編程開發(fā)之NotiFication用法。分享給大家供大家參考,具體如下:
notification就是通知的意思,安卓中指通知欄,一般用在電話,短信,郵件,鬧鐘鈴聲,在手機(jī)的狀態(tài)欄上就會出現(xiàn)一個小圖標(biāo),提示用戶處理這個快訊,這時手從上方滑動狀態(tài)欄就可以展開并處理這個快訊。
在幫助文檔中,是這么說的, notification類表示一個持久的通知,將提交給用戶使用NotificationManager。已添加的Notification.Builder,使其更容易構(gòu)建通知。
notification是一種讓你的應(yīng)用程序在沒有開啟情況下或在后臺運行警示用戶。它是看不見的程序組件(Broadcast Receiver,Service和不活躍的Activity)警示用戶有需要注意的事件發(fā)生的最好途徑。
先來區(qū)分以下狀態(tài)欄和狀態(tài)條的區(qū)別:
1、狀態(tài)條就是手機(jī)屏幕最上方的一個條形狀的區(qū)域;
在狀態(tài)條有好多信息量:比如usb連接圖標(biāo),手機(jī)信號圖標(biāo),電池電量圖標(biāo),時間圖標(biāo)等等;
2、狀態(tài)欄就是手從狀態(tài)條滑下來的可以伸縮的view;
在狀態(tài)欄中一般有兩類(使用FLAG_標(biāo)記):
(1)正在進(jìn)行的程序; (2)是通知事件;
一個Notification傳送的信息有:
1、一個狀態(tài)條圖標(biāo);
2、在拉伸的狀態(tài)欄窗口中顯示帶有大標(biāo)題,小標(biāo)題,圖標(biāo)的信息,并且有處理該點擊事件:比如調(diào)用該程序的入口類;
3、閃光,LED,或者震動;
下面對Notification類中的一些常量,字段,方法簡單介紹一下:
常量:
DEFAULT_ALL 使用所有默認(rèn)值,比如聲音,震動,閃屏等等
DEFAULT_LIGHTS 使用默認(rèn)閃光提示
DEFAULT_SOUNDS 使用默認(rèn)提示聲音
DEFAULT_VIBRATE 使用默認(rèn)手機(jī)震動
注意:在加入手機(jī)震動效果時一定要在項目清單文件中加入手機(jī)震動權(quán)限:
下面通過簡單額小實例來具體實現(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對象; notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } // 測試 public void test1(View v) { showNotification("來短信了", "5557", "hello!", R.drawable.ic_launcher, R.drawable.ic_launcher); } // 第二步:對Notification的一些屬性進(jìn)行設(shè)置比如:內(nèi)容,圖標(biāo),標(biāo)題,相應(yīng)notification的動作進(jìn)行處理等等; public void showNotification(String tickerText, String contentTitle, String contentText, int iconId, int notiId) { // 創(chuàng)建一個Notification Notification notification = new Notification(); // 設(shè)置通知 消息 圖標(biāo) notification.icon = iconId; // 設(shè)置發(fā)出消息的內(nèi)容 notification.tickerText = tickerText; // 設(shè)置發(fā)出通知的時間 notification.when = System.currentTimeMillis(); // 設(shè)置顯示通知時的默認(rèn)的發(fā)聲、振動、Light效果 notification.defaults = Notification.DEFAULT_VIBRATE;// 振動 // 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對象的notify方法 顯示Notification消息 需要制定 // Notification的標(biāo)識 notificationManager.notify(notiId, notification); } // 6步:使用notificationManager對象的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>
布局效果:
切記實現(xiàn)震動效果在清單文件中加入權(quán)限:
實現(xiàn)效果如下:
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Android中GPS坐標(biāo)轉(zhuǎn)換為高德地圖坐標(biāo)詳解
最近因為公司需求,在做GPS定位,并且將獲得的坐標(biāo)顯示在高德地圖上,但是實際效果跟我們期望的是有偏差的。通過查閱資料,才知道有地球坐標(biāo)、火星坐標(biāo)之說。下面這篇文章就詳細(xì)介紹了Android中GPS坐標(biāo)轉(zhuǎn)換為高德地圖坐標(biāo)的方法,需要的朋友可以參考下。2017-01-01Android實現(xiàn)雅虎新聞?wù)虞d視差動畫效果
這篇文章主要介紹了Android實現(xiàn)雅虎新聞?wù)虞d視差動畫效果,通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08Android中Fragment的加載方式與數(shù)據(jù)通信詳解
本文主要介紹了Android中Fragment的加載方式與數(shù)據(jù)通信的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03