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即可
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í)有所幫助,也希望大家多多支持腳本之家。
- Android使用Notification實(shí)現(xiàn)通知功能
- Android開發(fā)之Notification手機(jī)狀態(tài)欄通知用法實(shí)例分析
- Android使用Notification在狀態(tài)欄上顯示通知
- Android中Notification通知用法詳解
- Android 中Notification彈出通知實(shí)現(xiàn)代碼
- Android中使用Notification實(shí)現(xiàn)狀態(tài)欄的通知
- android 通知Notification詳解及實(shí)例代碼
- Android開發(fā)之Notification通知用法詳解
- Android Notification通知使用詳解
相關(guān)文章
Android中關(guān)于相對布局RelativeLayout的技巧匯總
RelativeLayout是相對布局控件,以控件之間相對位置或相對父容器位置進(jìn)行排列。下面這篇文章主要給大家介紹了關(guān)于Android中相對布局RelativeLayout的一些技巧,需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02Android 廣播監(jiān)聽網(wǎng)絡(luò)狀態(tài)詳解及實(shí)例代碼
這篇文章主要介紹了Android 廣播監(jiān)聽網(wǎng)絡(luò)狀態(tài)詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02Flutter利用Canvas模擬實(shí)現(xiàn)微信紅包領(lǐng)取效果
這篇文章主要為大家詳細(xì)介紹了如何利用Flutter中的Canvas模擬實(shí)現(xiàn)微信紅包領(lǐng)取的效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-03-03Android使用GestureOverlayView控件實(shí)現(xiàn)手勢識(shí)別
這篇文章主要為大家詳細(xì)介紹了Android使用GestureOverlayView控件實(shí)現(xiàn)手勢識(shí)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04Flutter通過Container實(shí)現(xiàn)時(shí)間軸效果
時(shí)間軸是前端UI經(jīng)常用到的效果,本文講解下Flutter如何通過Container實(shí)現(xiàn),感興趣的朋友可以了解下2021-05-05Android實(shí)現(xiàn)通訊錄效果——獲取手機(jī)號碼和姓名
這篇文章主要介紹了Android實(shí)現(xiàn)通訊錄效果——獲取手機(jī)號碼和姓名的相關(guān)資料,需要的朋友可以參考下2016-03-03