av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

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

Android 通知欄的使用方法

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

//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);二、創建渠道

在 Android 8.0 及更高版本上提供通知,需要在系統中注冊應用的通知渠道。

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);} }

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

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

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

//點擊時想要打開的界面 Intent intent = new Intent(this, AlertDetails.class); //一般點擊通知都是打開獨立的界面,為了避免添加到現有的activity棧中,可以設置下面的啟動方式 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); //創建activity類型的pendingIntent,還可以創建廣播等其他組件 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
相關文章:
主站蜘蛛池模板: 国产欧美日韩一区二区三区 | 黄色综合| 国产午夜精品理论片a大结局 | 97精品久久 | 天天综合久久 | 美女视频三区 | 欧美久久久 | 国产精品99久久久久久www | 亚洲国产一区二区视频 | 日韩精品一区二区不卡 | 国产美女一区二区 | 91视频播放 | 欧美最猛黑人 | 欧美一区二区三区一在线观看 | 欧美午夜一区二区三区免费大片 | 午夜精品一区二区三区在线播放 | 97久久精品午夜一区二区 | 黄色国产视频 | 亚洲一区二区精品视频在线观看 | 国产综合网站 | 精品久久久久一区二区国产 | 欧美成人性生活 | 国产一区二区三区视频 | 亚洲三区在线观看 | 99精品欧美一区二区三区综合在线 | 欧美一级www片免费观看 | 自拍偷拍精品 | 精精国产xxxx视频在线播放7 | 亚洲欧美中文日韩在线v日本 | 中文字幕亚洲一区二区va在线 | 一区二区三区视频免费观看 | 久久大陆 | 国产成人99久久亚洲综合精品 | 欧美日韩在线一区二区三区 | 亚洲精品区| 欧美啊v在线观看 | 欧美一级在线免费观看 | 一区二区三区视频在线观看 | 亚洲精品一区在线观看 | 成人在线一区二区三区 | 久久精品视频亚洲 |