我是靠谱客的博主 无聊日记本,最近开发中收集的这篇文章主要介绍android.app.RemoteServiceException: Bad notification for startForeground,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

错误提示:  

    android.app.RemoteServiceException: Bad notification for startForeground
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1973)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7562)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

  8.0后要加上channel来区分通知,故需要改为如下的写法:

/**
 * 创建Notification
 */
String CHANNEL_ID = "channel_id_01";
String CHANNEL_NAME = "channel_name_test";
int NOTIFICATION_ID = 1;
NotificationChannel notificationChannel;
Notification notification;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    notificationChannel = new NotificationChannel(CHANNEL_ID,
            CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
    notificationChannel.enableLights(true);
    notificationChannel.setLightColor(Color.RED);
    notificationChannel.setShowBadge(true);
    notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.createNotificationChannel(notificationChannel);

    notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("This is content title")
            .setContentText("This is content text")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
            .setContentIntent(pi)
            .build();
} else {
    notification = new Notification.Builder(this)
            .setContentTitle("This is content title")
            .setContentText("This is content text")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
            .setContentIntent(pi)
            .build();
}

/**
 * 设置notification在前台展示
 */
startForeground(NOTIFICATION_ID, notification);

 

最后

以上就是无聊日记本为你收集整理的android.app.RemoteServiceException: Bad notification for startForeground的全部内容,希望文章能够帮你解决android.app.RemoteServiceException: Bad notification for startForeground所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部