"The first line of code" Chapter X ServiceBestPractice error: Android 8.1 or more systems start to develop the service and error notification

Error is as follows : Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification

The reason : the 8.0 before informing Andrews is not broken down, in order to further optimize the management notice, Google released at 8:00 android notification has been modified to optimize, there has been notification channel function. Visible specific principles: use startForeground Bad notification for error occurs in the Notification in Android 8.0

So to join notification channel function, access to information that they can change a bit after debugging through, in this record to be a careful study be back. Reference Code: android.app.RemoteServiceException: Notification for startForeground of Bad: java.lang.RuntimeException

Need to change part : DownloadService-> getNotification Method:

    private Notification getNotification(String title, int progress) {
        String CHANNEL_ONE_ID = "com.primedu.cn";
        String CHANNEL_ONE_NAME = "Channel One";
        //NotificationChannel notificationChannel = null;
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"default");
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                //修改安卓8.1以上系统报错
                     NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID, CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_MIN);
                     notificationChannel.enableLights(false);//如果使用中的设备支持通知灯,则说明此通知通道是否应显示灯
                     notificationChannel.setShowBadge(false);//是否显示角标
                     notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
                     NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                    manager.createNotificationChannel(notificationChannel);
                    builder.setChannelId(CHANNEL_ONE_ID);
                    }
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        builder.setContentIntent(pi);
        builder.setContentTitle(title);
        Notification notification = builder.build(); // 获取构建好的Notification
        notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
        if (progress >= 0) {
            // 当progress大于或等于0时才需显示下载进度
            builder.setContentText(progress + "%");
            builder.setProgress(100, progress, false);
        }
        return builder.build();
    }

Explanation :

  1. channelId
    notification channel ID: can be any string can be globally unique
  2. channelName
    notice the name of the channel, this is visible to the user, the developer requires careful planning naming
发布了22 篇原创文章 · 获赞 4 · 访问量 3123

Guess you like

Origin blog.csdn.net/weixin_43633568/article/details/103771269