最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 通知欄的使用方法

 更新時間:2021年05月24日 08:49:13   作者:一條魚和一片海  
不同版本通知欄的創(chuàng)建方式不盡相同,當前官方推薦使用 NotificationCompat 相關的API,兼容到Android 4.0,但是部分新功能,比如內嵌回復操作,舊版本是無法支持的。

一、設置通知內容

	//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 通知欄的使用的資料請關注腳本之家其它相關文章!

相關文章

最新評論

鄂托克前旗| 松江区| 庄河市| 马关县| 固阳县| 舟曲县| 屏东县| 淳化县| 长沙市| 安溪县| 简阳市| 玉环县| 桂东县| 水城县| 准格尔旗| 九龙坡区| 凤台县| 淮阳县| 咸阳市| 崇礼县| 牡丹江市| 米易县| 上栗县| 化州市| 太谷县| 武穴市| 晋宁县| 西城区| 申扎县| 临泽县| 房山区| 天全县| 华宁县| 灌云县| 无极县| 定边县| 兖州市| 庄河市| 资中县| 元氏县| 太湖县|