国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Android 通知欄的使用方法

瀏覽:2日期:2022-09-18 14:31:22
目錄一、設置通知內容二、創(chuàng)建渠道三、設置通知欄的點擊操作四、顯示通知一、設置通知內容

//CHANNEL_ID,渠道ID,Android 8.0及更高版本必須要設置 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) //設置小圖標 .setSmallIcon(R.drawable.notification_icon) //設置標題 .setContentTitle(textTitle) //設置內容 .setContentText(textContent) //設置等級 .setPriority(NotificationCompat.PRIORITY_DEFAULT);二、創(chuàng)建渠道

在 Android 8.0 及更高版本上提供通知,需要在系統(tǒ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);} }

上述代碼應該在應用啟動時立即執(zhí)行,可以放在 Application 中進行初始化。

三、設置通知欄的點擊操作

一般點擊通知欄會打開對應的 Activity 界面,具體代碼如下:

//點擊時想要打開的界面 Intent intent = new Intent(this, AlertDetails.class); //一般點擊通知都是打開獨立的界面,為了避免添加到現(xiàn)有的activity棧中,可以設置下面的啟動方式 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) //設置pendingIntent .setContentIntent(pendingIntent) //設置點擊后是否自動消失 .setAutoCancel(true); 四、顯示通知

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); //notificationId 相當于通知的唯一標識,用于更新或者移除通知 notificationManager.notify(notificationId, builder.build());

還有很多特殊功能,可以直接查看官網教程進行設置。

以上就是Android 通知欄的使用方法的詳細內容,更多關于Android 通知欄的使用的資料請關注好吧啦網其它相關文章!

標簽: Android
相關文章:
主站蜘蛛池模板: 激情欧美日韩一区二区 | 亚洲久久天堂 | 久久久久久免费精品视频 | 日韩欧美一区二区不卡看片 | 福利视频99 | 国产国产人免费视频成69堂 | 99久久精品99999久久 | 欧美高清视频一区 | 国产偷自拍 | 在线观看国产一区二区三区99 | 国产亚洲精品片a77777 | 日韩毛片大全免费高清 | 久久男人的天堂 | 国产成人在线观看免费网站 | 精品国产v无码大片在线观看 | 99视频免费在线观看 | 亚洲精品国产一区二区 | 亚洲国产三级在线观看 | 欧美成人亚洲高清在线观看 | 九九免费精品视频 | 美女131爽爽爽做爰中文视频 | 亚洲乱码国产一区网址 | 国产在线综合视频 | 一级毛片在线视频 | 九九爱精品| 正在播放国产精品放孕妇 | 国产一级aaa全黄毛片 | 久久黄色免费网站 | 一级做a爱片特黄在线观看 一级做a爱片特黄在线观看免费看 | 国产一区二区三区免费 | 精品一久久香蕉国产线看播放 | 欧美一级精品高清在线观看 | 在线观看国产精成人品 | 欧美精品国产精品 | 一级做a爰片欧美一区 | 国产一区二区fc2ppv在线播放 | 一区二区三区四区免费视频 | 国产三级小视频 | 成人午夜视频免费观看 | 国产婷婷一区二区三区 | 亚洲欧美一区二区久久香蕉 |