欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開(kāi)發(fā)之Notification通知用法詳解

 更新時(shí)間:2016年11月16日 10:06:26   作者:陳達(dá)輝  
這篇文章主要介紹了Android開(kāi)發(fā)之Notification通知用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Notification通知的功能、參數(shù)、定義及使用方法,需要的朋友可以參考下

本文實(shí)例講述了Android開(kāi)發(fā)之Notification通知用法。分享給大家供大家參考,具體如下:

根據(jù)activity的生命周期,在activity不顯示時(shí),會(huì)執(zhí)行onStop函數(shù)(比如按下home鍵),所以你在onStop函數(shù)(按退出鍵除外)里面把notification放在通知欄里,再此顯示時(shí),把notification從通知欄里去掉?;蛘撸灰绦蛟谶\(yùn)行就一直顯示通知欄圖標(biāo)。

下面對(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)

【說(shuō)明】:加入手機(jī)震動(dòng),一定要在manifest.xml中加入權(quán)限:

<uses-permission android:name="android.permission.VIBRATE" />

以上的效果常量可以疊加,即通過(guò)

notification.defaults =DEFAULT_SOUND|DEFAULT_VIBRATE; 

notification.defaults |= DEFAULT_SOUND (最好在真機(jī)上測(cè)試,震動(dòng)效果模擬器上沒(méi)有)

設(shè)置flag位

FLAG_AUTO_CANCEL 該通知能被狀態(tài)欄的清除按鈕給清除掉
FLAG_NO_CLEAR 該通知能被狀態(tài)欄的清除按鈕給清除掉
FLAG_ONGOING_EVENT 通知放置在正在運(yùn)行
FLAG_INSISTENT 是否一直進(jìn)行,比如音樂(lè)一直播放,知道用戶響應(yīng)

常用字段:

contentIntent 設(shè)置PendingIntent對(duì)象,點(diǎn)擊時(shí)發(fā)送該Intent
defaults 添加默認(rèn)效果
flags 設(shè)置flag位,例如FLAG_NO_CLEAR等
icon 設(shè)置圖標(biāo)
sound 設(shè)置聲音
tickerText 顯示在狀態(tài)欄中的文字
when 發(fā)送此通知的時(shí)間戳

NotificationManager常用方法介紹:

public void cancelAll() 移除所有通知(只是針對(duì)當(dāng)前Context下的Notification)
public void cancel(int id) 移除標(biāo)記為id的通知 (只是針對(duì)當(dāng)前Context下的所有Notification)
public void notify(String tag ,int id, Notification notification) 將通知加入狀態(tài)欄,標(biāo)簽為tag,標(biāo)記為id
public void notify(int id, Notification notification) 將通知加入狀態(tài)欄,標(biāo)記為id

package com.ljq.activity;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
clearNotification();
}
@Override
protected void onStop() {
showNotification();
super.onStop();
}
@Override
protected void onStart() {
clearNotification();
super.onStart();
}
/**
* 在狀態(tài)欄顯示通知
*/
private void showNotification(){
// 創(chuàng)建一個(gè)NotificationManager的引用 
NotificationManager notificationManager = (NotificationManager) 
this.getSystemService(android.content.Context.NOTIFICATION_SERVICE); 
// 定義Notification的各種屬性 
Notification notification =new Notification(R.drawable.icon, 
"督導(dǎo)系統(tǒng)", System.currentTimeMillis()); 
//FLAG_AUTO_CANCEL 該通知能被狀態(tài)欄的清除按鈕給清除掉
//FLAG_NO_CLEAR 該通知不能被狀態(tài)欄的清除按鈕給清除掉
//FLAG_ONGOING_EVENT 通知放置在正在運(yùn)行
//FLAG_INSISTENT 是否一直進(jìn)行,比如音樂(lè)一直播放,知道用戶響應(yīng)
notification.flags |= Notification.FLAG_ONGOING_EVENT; // 將此通知放到通知欄的"Ongoing"即"正在運(yùn)行"組中 
notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在點(diǎn)擊了通知欄中的"清除通知"后,此通知不清除,經(jīng)常與FLAG_ONGOING_EVENT一起使用 
notification.flags |= Notification.FLAG_SHOW_LIGHTS; 
//DEFAULT_ALL 使用所有默認(rèn)值,比如聲音,震動(dòng),閃屏等等
//DEFAULT_LIGHTS 使用默認(rèn)閃光提示
//DEFAULT_SOUNDS 使用默認(rèn)提示聲音
//DEFAULT_VIBRATE 使用默認(rèn)手機(jī)震動(dòng),需加上<uses-permission android:name="android.permission.VIBRATE" />權(quán)限
notification.defaults = Notification.DEFAULT_LIGHTS; 
//疊加效果常量
//notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND;
notification.ledARGB = Color.BLUE; 
notification.ledOnMS =5000; //閃光時(shí)間,毫秒
// 設(shè)置通知的事件消息 
CharSequence contentTitle ="督導(dǎo)系統(tǒng)標(biāo)題"; // 通知欄標(biāo)題 
CharSequence contentText ="督導(dǎo)系統(tǒng)內(nèi)容"; // 通知欄內(nèi)容 
Intent notificationIntent =new Intent(MainActivity.this, MainActivity.class); // 點(diǎn)擊該通知后要跳轉(zhuǎn)的Activity 
PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
notification.setLatestEventInfo(this, contentTitle, contentText, contentItent); 
// 把Notification傳遞給NotificationManager 
notificationManager.notify(0, notification); 
}
//刪除通知 
private void clearNotification(){
// 啟動(dòng)后刪除之前我們定義的通知 
NotificationManager notificationManager = (NotificationManager) this 
.getSystemService(NOTIFICATION_SERVICE); 
notificationManager.cancel(0); 
}
}

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android編程開(kāi)發(fā)之SD卡操作方法匯總》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論