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

android通知欄的實(shí)現(xiàn)方法分析

 更新時(shí)間:2016年08月13日 11:46:25   作者:llyofdream  
這篇文章主要介紹了android通知欄的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了系統(tǒng)Notification及自定義Notification兩種實(shí)現(xiàn)技巧與相關(guān)操作步驟,需要的朋友可以參考下

本文實(shí)例講述了android通知欄的實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:

這幾天一直在修改twigee的源代碼,其中一個(gè)要加入的功能是常駐Notification欄,以前寫的時(shí)候只能出現(xiàn) 在“通知”這一組中,想把它放在“正在運(yùn)行”組中卻不知道怎么放,查了下官方文檔,找到了方法,在notification的flags字段中加一下 “FLAG_ONGOING_EVENT”就可以了。同時(shí)我也把Notification的使用方法給總結(jié)了一下。詳見下文:

(1)、使用系統(tǒng)定義的Notification

以下是使用示例代碼:

//創(chuàng)建一個(gè)NotificationManager的引用
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);
// 定義Notification的各種屬性
int icon = R.drawable.icon; //通知圖標(biāo)
CharSequence tickerText = "Hello"; //狀態(tài)欄顯示的通知文本提示
long when = System.currentTimeMillis(); //通知產(chǎn)生的時(shí)間,會(huì)在通知信息里顯示
//用上面的屬性初始化 Nofification
Notification notification = new Notification(icon,tickerText,when);
/*
* 添加聲音
* notification.defaults |=Notification.DEFAULT_SOUND;
* 或者使用以下幾種方式
* notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
* notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
* 如果想要讓聲音持續(xù)重復(fù)直到用戶對(duì)通知做出反應(yīng),則可以在notification的flags字段增加"FLAG_INSISTENT"
* 如果notification的defaults字段包括了"DEFAULT_SOUND"屬性,則這個(gè)屬性將覆蓋sound字段中定義的聲音
*/
/*
* 添加振動(dòng)
* notification.defaults |= Notification.DEFAULT_VIBRATE;
* 或者可以定義自己的振動(dòng)模式:
* long[] vibrate = {0,100,200,300}; //0毫秒后開始振動(dòng),振動(dòng)100毫秒后停止,再過200毫秒后再次振動(dòng)300毫秒
* notification.vibrate = vibrate;
* long數(shù)組可以定義成想要的任何長度
* 如果notification的defaults字段包括了"DEFAULT_VIBRATE",則這個(gè)屬性將覆蓋vibrate字段中定義的振動(dòng)
*/
/*
* 添加LED燈提醒
* notification.defaults |= Notification.DEFAULT_LIGHTS;
* 或者可以自己的LED提醒模式:
* notification.ledARGB = 0xff00ff00;
* notification.ledOnMS = 300; //亮的時(shí)間
* notification.ledOffMS = 1000; //滅的時(shí)間
* notification.flags |= Notification.FLAG_SHOW_LIGHTS;
*/
/*
* 更多的特征屬性
* notification.flags |= FLAG_AUTO_CANCEL; //在通知欄上點(diǎn)擊此通知后自動(dòng)清除此通知
* notification.flags |= FLAG_INSISTENT; //重復(fù)發(fā)出聲音,直到用戶響應(yīng)此通知
* notification.flags |= FLAG_ONGOING_EVENT; //將此通知放到通知欄的"Ongoing"即"正在運(yùn)行"組中
* notification.flags |= FLAG_NO_CLEAR; //表明在點(diǎn)擊了通知欄中的"清除通知"后,此通知不清除,
* //經(jīng)常與FLAG_ONGOING_EVENT一起使用
* notification.number = 1; //number字段表示此通知代表的當(dāng)前事件數(shù)量,它將覆蓋在狀態(tài)欄圖標(biāo)的頂部
* //如果要使用此字段,必須從1開始
* notification.iconLevel = ; //
*/
//設(shè)置通知的事件消息
Context context = getApplicationContext(); //上下文
CharSequence contentTitle = "My Notification"; //通知欄標(biāo)題
CharSequence contentText = "Hello World!"; //通知欄內(nèi)容
Intent notificationIntent = new Intent(this,Main.class); //點(diǎn)擊該通知后要跳轉(zhuǎn)的Activity
PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
//把Notification傳遞給 NotificationManager
mNotificationManager.notify(0,notification);

如果想要更新一個(gè)通知,只需要在設(shè)置好notification之后,再次調(diào)用 setLatestEventInfo(),然后重新發(fā)送一次通知即可,即再次調(diào)用notify()。

(2)、使用自定義的 Notification

要 創(chuàng)建一個(gè)自定義的Notification,可以使用RemoteViews。要定義自己的擴(kuò)展消息,首先 要初始化一個(gè)RemoteViews對(duì)象,然后將它傳遞給Notification的contentView字段,再把PendingIntent傳遞給 contentIntent字段。以下示例代碼是完整步驟:

1、創(chuàng)建一個(gè)自 定義的消息布局 view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView android:id="@+id/image" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_marginRight="10dp" />
<TextView android:id="@+id/text" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:textColor="#000" />
</LinearLayout>

2、 在程序代碼中使用RemoteViews的方法來定義image和text。然后把RemoteViews對(duì)象傳到contentView字段

RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.view);
contentView.setImageViewResource(R.id.image,R.drawable.icon);
contentView.setTextViewText(R.id.text,”Hello,this message is in a custom expanded view”);
notification.contentView = contentView;

3、 為Notification的contentIntent字段定義一個(gè)Intent(注意,使用自定義View不需要 setLatestEventInfo()方法)

Intent notificationIntent = new Intent(this,Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
notification.contentIntent = contentIntent;

4、發(fā)送通知

mNotificationManager.notify(2,notification);

以下是全部示例代碼:

//創(chuàng)建一個(gè) NotificationManager的引用
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);
// 定義Notification的各種屬性
int icon = R.drawable.icon; //通知圖標(biāo)
CharSequence tickerText = "Hello"; //狀態(tài)欄顯示的通知文本提示
long when = System.currentTimeMillis(); //通知產(chǎn)生的時(shí)間,會(huì)在通知信息里顯示
//用上面的屬性初始化 Nofification
Notification notification = new Notification(icon,tickerText,when);
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.view);
contentView.setImageViewResource(R.id.image, R.drawable.iconempty);
contentView.setTextViewText(R.id.text, "Hello,this is JC");
notification.contentView = contentView;
Intent notificationIntent = new Intent(this,Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
notification.contentIntent = contentIntent;
//把Notification傳遞給NotificationManager
mNotificationManager.notify(0,notification);

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

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

相關(guān)文章

最新評(píng)論