Android開發(fā)系列(二十四):Notification的功能與用法
來源:程序員人生 發(fā)布時(shí)間:2014-11-20 08:57:45 閱讀次數(shù):3181次
關(guān)于消息的提示有兩種:1種是Toast,1種就是Notification。前者保持的時(shí)間比較短暫,后者保持的時(shí)間比較長(zhǎng)。
而且我們平常手機(jī)的利用比如網(wǎng)易、貼吧等等都有很多的推送消息,就是用Notification實(shí)現(xiàn)的。
Notification是顯示在手機(jī)狀態(tài)欄的通知―手機(jī)狀態(tài)欄位于手機(jī)屏幕的上方。程序1般通過NotificationManager服務(wù)來發(fā)送Notification通知
Notification的1些方法,接下來我們都能夠用到:
setDefaults():設(shè)置通知LED等、音樂、震動(dòng)等等。
setAutoCancel():設(shè)置點(diǎn)擊通知后,狀態(tài)欄自動(dòng)刪除通知。
setContentTitle():設(shè)置通知的標(biāo)題
setContentText():設(shè)置通知的內(nèi)容
setTicker():設(shè)置通知的提示信息
setSmallIcon():為通知設(shè)置圖標(biāo)(注意這個(gè)方法第3個(gè)是i的大寫,不是L的小寫)
發(fā)送Notification的步驟:
1、調(diào)用getSystemService(NOTIFICATION_SERVICE)方法獲得系統(tǒng)的Notification Manager服務(wù)
2、通過構(gòu)造器創(chuàng)建1個(gè)Notification對(duì)象。
3、為Notification設(shè)置各種屬性。
4、通過NotificationManager發(fā)送Notification。
在這里,我們要注意1點(diǎn)要在AndroidManifest.xml文件中添加幾個(gè)權(quán)限:
<!-- 添加操作閃光燈的權(quán)限 -->
<uses-permission android:name="android.permission.FLASHLIGHT" />
<!-- 添加操作振動(dòng)器的權(quán)限 -->
<uses-permission android:name="android.permission.VIBRATE" />
接下來,我們通過具體的代碼來講明。
main.xml:
<span style="font-size:14px;"><?xml version="1.0" encoding="utf⑻"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="發(fā)送Notification"
android:onClick="send"
/>
</LinearLayout>
</span>
這里設(shè)置了1個(gè)按鈕,點(diǎn)擊會(huì)發(fā)送通知
然后,我們看下NotificationTest.java的代碼:
<span style="font-size:14px;">package cn.notificationtest.com;
import cn.notificationtest.com.R;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
public class NotificationTest extends Activity
{
static final int NOTIFICATION_ID = 0x123;
NotificationManager nm;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 獲得系統(tǒng)的NotificationManager服務(wù)
nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
}
// 為發(fā)送通知的按鈕的點(diǎn)擊事件定義事件處理方法
public void send(View source)
{
// 創(chuàng)建1個(gè)啟動(dòng)其他Activity的Intent
Intent intent = new Intent(NotificationTest.this
, OtherActivity.class);
PendingIntent pi = PendingIntent.getActivity(
NotificationTest.this, 0, intent, 0);
Notification notify = new Notification.Builder(this)
// 設(shè)置打開該通知,該通知自動(dòng)消失
.setAutoCancel(true)
// 設(shè)置顯示在狀態(tài)欄的通知提示信息
.setTicker("網(wǎng)易新聞")
// 設(shè)置通知的圖標(biāo)
.setSmallIcon(R.drawable.notify)
// 設(shè)置通知內(nèi)容的標(biāo)題
.setContentTitle("這是新聞標(biāo)題")
// 設(shè)置通知內(nèi)容
.setContentText("這是新聞的內(nèi)容:*************")
// // 設(shè)置使用系統(tǒng)默許的聲音、默許LED燈
// .setDefaults(Notification.DEFAULT_SOUND
// |Notification.DEFAULT_LIGHTS)
// 設(shè)置通知的自定義聲音
.setSound(Uri.parse("android.resource://cn.notificationtest.com/"+R.raw.msg))
.setWhen(System.currentTimeMillis())
// 設(shè)改通知將要啟動(dòng)程序的Intent
.setContentIntent(pi).getNotification();
// 發(fā)送通知
nm.notify(NOTIFICATION_ID, notify);
}
}</span>
在這個(gè)java文件中,我們通過構(gòu)造器創(chuàng)建了1個(gè)Notification對(duì)象。然后為Notification設(shè)置各種屬性。最后通過NotificationManager發(fā)送Notification。
(這里需要注意的1點(diǎn)是,我們定義的聲音,圖標(biāo)甚么的都是個(gè)人創(chuàng)建)
通過上邊的java代碼,我們創(chuàng)建了1個(gè)Intent對(duì)象,可以通過這條通知,切換到另外的1個(gè)Activity界面:OtherActivity
<span style="font-size:14px;">/**
*
*/
package cn.notificationtest.com;
import cn.notificationtest.com.R;
import android.app.Activity;
import android.os.Bundle;
public class OtherActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//設(shè)置該Activity顯示的頁面
setContentView(R.layout.other);
}
}
</span>
效果圖以下所示:

生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)