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

Android中通知Notification的使用方法

 更新時(shí)間:2016年08月23日 09:39:01   作者:bingjianIT  
這篇文章主要為大家詳細(xì)介紹了Android中通知Notification的使用方法,感興趣的小伙伴們可以參考一下

每個(gè)使用Android手機(jī)的人應(yīng)該對Android中的通知不陌生,下面我們就學(xué)習(xí)一下怎么使用Android中的通知。

一、通知的基本用法

活動(dòng)、廣播接收器和服務(wù)中都可以創(chuàng)建通知,由于我們一般在程序進(jìn)入后臺(tái)后才使用通知,所以真實(shí)場景中,一般很少在活動(dòng)中創(chuàng)建通知。

1、第一行代碼上面介紹的創(chuàng)建通知的方法

//獲得通知管理器
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)
//創(chuàng)建通知對象,參數(shù)依次為通知圖標(biāo)、ticker(通知欄上一閃而過的信息)、通知?jiǎng)?chuàng)建時(shí)間
Notification notification = new Notification(R.drawable. ic_launcher, "This is ticker text", System.currentTimeMillis());
//設(shè)置通知布局,參數(shù)依次為Context,通知標(biāo)題、通知正文、PindingIntent對象(點(diǎn)擊通知之后的事件處理)
notification.setLatestEventInfo(this, "This is content title", "This is content text", null);
//顯示通知,參數(shù)依次為唯一的id、通知對象
manager.notify(1, notification);

注:上面的方法現(xiàn)在已經(jīng)被廢棄,當(dāng)API Level為11及之前時(shí)使用此方法 

2、APILevel高于11低于16的可以用下面的方法創(chuàng)建通知

//1、獲得通知管理器
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//創(chuàng)建Builder,設(shè)置屬性
Notification.Builder builder = new Notification.Builder(this)
    .setAutoCancel(true)
    .setContentTitle("title")
    .setContentText("describe")
    .setSmallIcon(R.drawable.ic_launcher)
    .setWhen(System.currentTimeMillis())
    .setOngoing(true);
//獲得Notification對象
Notification notification = builder.getNotification();
//顯示通知
manager.notify(1, notification);

3、API Level在16及以上,使用下面的方法創(chuàng)建通知

//1、獲得通知管理器
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//創(chuàng)建Builder,設(shè)置屬性
Notification notification = new Notification.Builder(this)
    .setAutoCancel(true)
    .setContentTitle("title")
    .setContentText("describe")
    .setSmallIcon(R.drawable.ic_launcher)
    .setWhen(System.currentTimeMillis())
    .setOngoing(true)
    .build();
//顯示通知
manager.notify(1, notification);

二、響應(yīng)通知的點(diǎn)擊事件

我們通過 PendingIntent對象響應(yīng)容通知的點(diǎn)擊事件
 1、獲得PendingIntent對象

PendingIntent用來處理通知的“意圖”。我們需要先構(gòu)造一個(gè)Intent對象,然后再通過PendingIntent.getActivity()、PendingIntent.gBroadcast()、PendingIntent.getService()來啟動(dòng)執(zhí)行不同的意圖。這三個(gè)靜態(tài)方法傳入的參數(shù)相同,第一個(gè)為Context,第二個(gè)參數(shù)一般傳入0,第三個(gè)參數(shù)為Intent對象,第四個(gè)參數(shù)指定PendingIntent的行為,有FLAG_ONE_SHOT、FLAG_NO_CREATE、FLAG_CANCEL_CURRENT和FLAG_UPDATE_ CURRENT這四種值可選。 

2、設(shè)置PendingIntent

通過setContentIntent(pendingIntent)來設(shè)置。 

下面是一個(gè)簡單的例子

//獲得通知管理器
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//構(gòu)造Intent對象
Intent intent = new Intent(MainActivity.this, TestActivity.class);
//獲得PendingIntent對象
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//創(chuàng)建Builder,設(shè)置屬性
Notification notification = new Notification.Builder(this)
    .setAutoCancel(true)
    .setContentTitle("title")
    .setContentText("describe")
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentIntent(pendingIntent)  //設(shè)置PendingIntent
    .setWhen(System.currentTimeMillis())
    .setOngoing(true)
    .build();
//顯示通知
manager.notify(1, notification);

三、取消通知

取消通知只需要在cancel()方法中傳入我們創(chuàng)建通知時(shí)指定的id即可 

復(fù)制代碼 代碼如下:
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(1);

四、通知的高級用法

1、通知到來時(shí)播放音頻

Notification有一個(gè)屬性是sound,這里需要傳入音頻對應(yīng)的URI

 //音頻Uri
Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones"));
setSound(soundUri);

2、通知到來時(shí)手機(jī)振動(dòng)

我們使用vibrate這個(gè)屬性讓通知到來時(shí)控制手機(jī)振動(dòng)。vibrate需要一個(gè)長整型數(shù)組,用于設(shè)置手機(jī)靜止和振動(dòng)的時(shí)長,單位為毫秒。下標(biāo)為偶數(shù)的表示手機(jī)靜止的時(shí)長,下標(biāo)為奇數(shù)為手機(jī)振動(dòng)的時(shí)長。

 //手機(jī)振動(dòng)靜止設(shè)置(靜止0秒,振動(dòng)一秒,靜止一秒,振動(dòng)一秒)
long[] vibrate = {0, 1000, 1000, 1000};
setVibrate(vibrate)

注:控制手機(jī)還需要在AndroidManifest.xml中聲明權(quán)限:

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

3、通知到來時(shí)閃爍LED燈

LED燈的使用涉及到以下一個(gè)屬性:
ledARGB ——- 控制LED燈的顏色
ledOnMS ——- 控制LED燈亮起的時(shí)間,以毫秒為單位
ledOffMS ——– 控制LED燈熄滅的時(shí)間,以毫秒為單位
主要通過setLights()方法依次對這三個(gè)屬性進(jìn)行設(shè)置 

setLights(Color.BLUE, 1000, 1000)

上面的代碼就是讓LED燈以藍(lán)色一閃一閃
4、通知到來時(shí)以默認(rèn)方式提醒

如果我們不想手動(dòng)設(shè)置這么多屬性,可以使用下面的方式 
.setDefaults(Notification.DEFAULT_ALL)

設(shè)置默認(rèn)值,由手機(jī)環(huán)境來決定在通知到來時(shí)播放什么鈴聲,如何振動(dòng),如何閃爍LED燈
最后說明一點(diǎn),手機(jī)播放鈴聲、手機(jī)振動(dòng)、LED燈的閃爍都需要真機(jī)調(diào)試,模擬器上是看不出效果的。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論