Android 通知欄的使用方法
一、設(shè)置通知內(nèi)容
//CHANNEL_ID,渠道ID,Android 8.0及更高版本必須要設(shè)置
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
//設(shè)置小圖標(biāo)
.setSmallIcon(R.drawable.notification_icon)
//設(shè)置標(biāo)題
.setContentTitle(textTitle)
//設(shè)置內(nèi)容
.setContentText(textContent)
//設(shè)置等級
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
二、創(chuàng)建渠道
在 Android 8.0 及更高版本上提供通知,需要在系統(tǒng)中注冊應(yīng)用的通知渠道。
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
//不同的重要程度會影響通知顯示的方式
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
上述代碼應(yīng)該在應(yīng)用啟動(dòng)時(shí)立即執(zhí)行,可以放在 Application 中進(jìn)行初始化。
三、設(shè)置通知欄的點(diǎn)擊操作
一般點(diǎn)擊通知欄會打開對應(yīng)的 Activity 界面,具體代碼如下:
//點(diǎn)擊時(shí)想要打開的界面
Intent intent = new Intent(this, AlertDetails.class);
//一般點(diǎn)擊通知都是打開獨(dú)立的界面,為了避免添加到現(xiàn)有的activity棧中,可以設(shè)置下面的啟動(dòng)方式
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//創(chuàng)建activity類型的pendingIntent,還可以創(chuàng)建廣播等其他組件
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
//設(shè)置pendingIntent
.setContentIntent(pendingIntent)
//設(shè)置點(diǎn)擊后是否自動(dòng)消失
.setAutoCancel(true);
四、顯示通知
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
//notificationId 相當(dāng)于通知的唯一標(biāo)識,用于更新或者移除通知
notificationManager.notify(notificationId, builder.build());
還有很多特殊功能,可以直接查看官網(wǎng)教程進(jìn)行設(shè)置。
以上就是Android 通知欄的使用方法的詳細(xì)內(nèi)容,更多關(guān)于Android 通知欄的使用的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android基于Sensor感應(yīng)器獲取重力感應(yīng)加速度的方法
這篇文章主要介紹了Android基于Sensor感應(yīng)器獲取重力感應(yīng)加速度的方法,涉及Android使用Sensor類實(shí)現(xiàn)感應(yīng)重力變化的功能,需要的朋友可以參考下2015-12-12
Android為按鈕控件綁定事件的五種實(shí)現(xiàn)方式
本篇文章主要是介紹了Android為按鈕控件綁定事件的五種實(shí)現(xiàn)方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11
Android Studio 3.6中新的視圖綁定工具ViewBinding 用法詳解
這篇文章主要介紹了Android Studio 3.6中新的視圖綁定工具ViewBinding 用法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Android雙擊返回鍵退出程序的實(shí)現(xiàn)方法
這篇文章主要介紹了Android雙擊返回鍵退出程序的實(shí)現(xiàn)方法,是Android程序開發(fā)中非常具有實(shí)用價(jià)值的重要技巧,需要的朋友可以參考下2014-09-09
Android使用TabLayou+fragment+viewpager實(shí)現(xiàn)滑動(dòng)切換頁面效果
這篇文章主要介紹了Android使用TabLayou+fragment+viewpager實(shí)現(xiàn)滑動(dòng)切換頁面效果,需要的朋友可以參考下2017-05-05
利用SpannableString和ImageSpan在textview中插入圖片的方法
這篇文章主要為大家詳細(xì)介紹了利用SpannableString和ImageSpan在textview中插入圖片的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Android 應(yīng)用的全屏和非全屏實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 應(yīng)用的全屏和非全屏實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05

