亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android中通知Notification的使用方法

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

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

一、通知的基本用法

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

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

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

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

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)通知的點擊事件

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

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

2、設(shè)置PendingIntent

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

下面是一個簡單的例子

//獲得通知管理器
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)建通知時指定的id即可 

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

四、通知的高級用法

1、通知到來時播放音頻

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

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

2、通知到來時手機振動

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

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

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

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

3、通知到來時閃爍LED燈

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

setLights(Color.BLUE, 1000, 1000)

上面的代碼就是讓LED燈以藍色一閃一閃
4、通知到來時以默認方式提醒

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

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

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

相關(guān)文章

最新評論