1. 程式人生 > >安卓8.0 O系統頂部通知提示狀態列

安卓8.0 O系統頂部通知提示狀態列

安卓8.0系統和8.0以下系統顯示彈出通知狀態列方法。

        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //建立notification
        builder = new Notification.Builder(this)
        .setOngoing(true)//正在執行
        .setWhen(System.currentTimeMillis())//什麼時候彈出通知欄
        .setContentTitle("title")
        .setContentText("描述")
        .setAutoCancel(false)//是否自動消失
        .setSmallIcon(R.mipmap.logoz);//設定小圖示

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //8.0以上彈出通知狀態列
            String channelID = "0";
            String channelName = "channel_name";
            NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
                builder.setChannelId(channelID);
                builder.build();
            }
        }else {
            //彈出通知欄 8.0以下系統彈出方式
            if (notificationManager != null) {
                notificationManager.notify(0, builder.build());
            }
        }
如果targetSdkVersion適配到26的話,也就是8.0系統。在全量更新apk的時候, 別忘記在清單檔案裡面新增
   <!--安卓8.0系統安裝app 未知來源許可權-->
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />