Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MediaStyle:RemoteServiceException:从包发布的错误通知_Java_Android_Notifications_Android Notifications_Huawei Mobile Services - Fatal编程技术网

Java MediaStyle:RemoteServiceException:从包发布的错误通知

Java MediaStyle:RemoteServiceException:从包发布的错误通知,java,android,notifications,android-notifications,huawei-mobile-services,Java,Android,Notifications,Android Notifications,Huawei Mobile Services,我正在尝试使用下面的代码在我的应用程序中创建一个通知媒体控制器,该控制器在所有设备上运行良好,除了华为P8 Lite和安卓5.0,我从Firebase测试实验室获得以下错误日志: android.app.RemoteServiceException:从发布的错误通知 包maa.app\u应用程序:无法展开以下项的RemoteView: 状态通知(pkg=maa.app\u app user=UserHandle{0}id=555标记=null分数=10 key=0 | maa.app|u app

我正在尝试使用下面的代码在我的应用程序中创建一个通知媒体控制器,该控制器在所有设备上运行良好,除了华为P8 Lite安卓5.0,我从Firebase测试实验室获得以下错误日志:

android.app.RemoteServiceException:从发布的错误通知 包maa.app\u应用程序:无法展开以下项的RemoteView: 状态通知(pkg=maa.app\u app user=UserHandle{0}id=555标记=null分数=10 key=0 | maa.app|u app | 555 | null | 10108:通知(pri=1 contentView=maa.app\u app/0x109007f振动=null 声音=空默认值=0x0标志=0x62颜色=0xFFBF类别=传输 actions=2 vis=PUBLIC)致命异常:主进程: maa.app_app,PID:18793 android.app.RemoteServiceException:从发布的错误通知 包maa.app\u应用程序:无法展开以下项的RemoteView: 状态通知(pkg=maa.app\u app user=UserHandle{0}id=555标记=null分数=10 key=0 | maa.app|u app | 555 | null | 10108:通知(pri=1 contentView=maa.app\u app/0x109007f振动=null 声音=空默认值=0x0标志=0x62颜色=0xFFBF类别=传输 行动=2对=公开 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1534) 位于android.os.Handler.dispatchMessage(Handler.java:102) Looper.loop(Looper.java:135)位于 android.app.ActivityThread.main(ActivityThread.java:5538)位于 java.lang.reflect.Method.invoke(本机方法)位于 java.lang.reflect.Method.invoke(Method.java:372)位于 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) 位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

这是我的密码:

void startNotify(Context context, String playbackStatus, String title) {
    String titlesonge;
    String artist;
    try {
        titlesonge = StringUtils.substringBefore(title, " - ");
        artist = StringUtils.substringAfter(title, " - ");
    } catch (Exception e) {
        titlesonge = title.substring(0, title.indexOf(" - "));
        artist = title.substring(title.lastIndexOf(" - ") - 1);
    }
    int icon = R.drawable.ic_pause_white;
    Intent playbackAction = new Intent(service, RadioService.class);
    playbackAction.setAction(RadioService.ACTION_PAUSE);
    PendingIntent action = PendingIntent.getService(service, 1, playbackAction, 0);
    if (playbackStatus.equals(PlaybackStatus.PAUSED)) {
        icon = R.drawable.ic_play_white;
        playbackAction.setAction(RadioService.ACTION_PLAY);
        action = PendingIntent.getService(service, 2, playbackAction, 0);

    }
    Intent stopIntent = new Intent(service, RadioService.class);
    stopIntent.setAction(RadioService.ACTION_STOP);
    PendingIntent stopAction = PendingIntent.getService(service, 3, stopIntent, 0);

    Intent intent = new Intent(service, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP |
            Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(service, 0, intent, 0);
    notificationManager.cancel(NOTIFICATION_ID);
    String PRIMARY_CHANNEL = "PRIMARY_CHANNEL_ID";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager manager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
        String PRIMARY_CHANNEL_NAME = "PRIMARY";
        NotificationChannel channel = new NotificationChannel(PRIMARY_CHANNEL, PRIMARY_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        if (manager != null) {
            manager.createNotificationChannel(channel);
        }
    }
    NotificationCompat.Builder builder = new NotificationCompat.Builder(service, PRIMARY_CHANNEL)
            .setAutoCancel(false)
            .setContentTitle(titlesonge)
            .setContentText(artist)
            .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.largeicon))
            .setContentIntent(pendingIntent)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setSmallIcon(R.drawable.smallwidth)
            .setColor(ContextCompat.getColor(context, R.color.colorneeded))
            .addAction(icon, "pause", action)
            .addAction(R.drawable.ic_stop_white, "stop", stopAction)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setWhen(System.currentTimeMillis())
            .setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
                    .setMediaSession(service.getMediaSession().getSessionToken())
                    .setShowActionsInCompactView(0, 1)
                    .setShowCancelButton(true)
                    .setCancelButtonIntent(stopAction));
    service.startForeground(NOTIFICATION_ID, builder.build());
}

有人能帮我解决这个问题吗?

由于某种原因,使用
.setStyle()
方法时,使用Android 5.0的华为设备会崩溃,因此您有两种可能性:

1-检测设备制造商是否为华为,是否拥有Android 5.0或更低版本

public boolean isLolliPopHuawei() {
    return (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1 ||
            android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) && Build.MANUFACTURER.equalsIgnoreCase("HUAWEI");
}
2-改用
PlayerNotificationManager

void exoPlayerNotification(Context context, SimpleExoPlayer exoPlayer, String title) {
    String titlesonge;
    String artist;
    try {
        titlesonge = StringUtils.substringBefore(title, " - ");
        artist = StringUtils.substringAfter(title, " - ");
    } catch (Exception e) {
        titlesonge = title.substring(0, title.indexOf(" - "));
        artist = title.substring(title.lastIndexOf(" - ") - 1);
    }
    String finalArtist = artist;
    String finalTitlesonge = titlesonge;
    mPlayerNotificationManager = PlayerNotificationManager.createWithNotificationChannel(
            context,
            "PRIMARY_CHANNEL_ID",
            R.string.plaza,
            NOTIFICATION_ID,
            new PlayerNotificationManager.MediaDescriptionAdapter() {
                @Override
                public String getCurrentContentTitle(Player player) {
                    return finalArtist;
                }

                @Nullable
                @Override
                public PendingIntent createCurrentContentIntent(Player player) {
                    Intent intent = new Intent(service, MainActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    return PendingIntent.getActivity(service, 0, intent,
                            PendingIntent.FLAG_UPDATE_CURRENT);
                }

                @Override
                public String getCurrentContentText(Player player) {
                    return finalTitlesonge;
                }

                @Nullable
                @Override
                public Bitmap getCurrentLargeIcon(Player player, PlayerNotificationManager.BitmapCallback callback) {
                    return BitmapFactory.decodeResource(service.getResources(), R.drawable.largeicon);
                }

                @Nullable
                @Override
                public String getCurrentSubText(Player player) {
                    return null;
                }
            }
    );
    mPlayerNotificationManager.setUseNavigationActions(false);
    mPlayerNotificationManager.setFastForwardIncrementMs(0);
    mPlayerNotificationManager.setRewindIncrementMs(0);
    mPlayerNotificationManager.setColorized(true);
    mPlayerNotificationManager.setColor(0xFFEEEEEE);
    mPlayerNotificationManager.setUseChronometer(true);
    mPlayerNotificationManager.setOngoing(true);
    mPlayerNotificationManager.setPriority(NotificationCompat.PRIORITY_MAX);
    mPlayerNotificationManager.setUsePlayPauseActions(true);
    mPlayerNotificationManager.setSmallIcon(R.drawable.smallwidth);
    mPlayerNotificationManager.setNotificationListener(new PlayerNotificationManager.NotificationListener() {
        @Override
        public void onNotificationStarted(int notificationId, Notification notification) {
            service.startForeground(notificationId, notification);
        }

        @Override
        public void onNotificationCancelled(int notificationId) {
            service.stopSelf();
            cancelNotify();
        }
    });
    mPlayerNotificationManager.setPlayer(exoPlayer);
}

由于某些原因,使用
.setStyle()
方法时,使用Android 5.0的华为设备会崩溃,因此有两种可能性:

1-检测设备制造商是否为华为,是否拥有Android 5.0或更低版本

public boolean isLolliPopHuawei() {
    return (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1 ||
            android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) && Build.MANUFACTURER.equalsIgnoreCase("HUAWEI");
}
2-改用
PlayerNotificationManager

void exoPlayerNotification(Context context, SimpleExoPlayer exoPlayer, String title) {
    String titlesonge;
    String artist;
    try {
        titlesonge = StringUtils.substringBefore(title, " - ");
        artist = StringUtils.substringAfter(title, " - ");
    } catch (Exception e) {
        titlesonge = title.substring(0, title.indexOf(" - "));
        artist = title.substring(title.lastIndexOf(" - ") - 1);
    }
    String finalArtist = artist;
    String finalTitlesonge = titlesonge;
    mPlayerNotificationManager = PlayerNotificationManager.createWithNotificationChannel(
            context,
            "PRIMARY_CHANNEL_ID",
            R.string.plaza,
            NOTIFICATION_ID,
            new PlayerNotificationManager.MediaDescriptionAdapter() {
                @Override
                public String getCurrentContentTitle(Player player) {
                    return finalArtist;
                }

                @Nullable
                @Override
                public PendingIntent createCurrentContentIntent(Player player) {
                    Intent intent = new Intent(service, MainActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    return PendingIntent.getActivity(service, 0, intent,
                            PendingIntent.FLAG_UPDATE_CURRENT);
                }

                @Override
                public String getCurrentContentText(Player player) {
                    return finalTitlesonge;
                }

                @Nullable
                @Override
                public Bitmap getCurrentLargeIcon(Player player, PlayerNotificationManager.BitmapCallback callback) {
                    return BitmapFactory.decodeResource(service.getResources(), R.drawable.largeicon);
                }

                @Nullable
                @Override
                public String getCurrentSubText(Player player) {
                    return null;
                }
            }
    );
    mPlayerNotificationManager.setUseNavigationActions(false);
    mPlayerNotificationManager.setFastForwardIncrementMs(0);
    mPlayerNotificationManager.setRewindIncrementMs(0);
    mPlayerNotificationManager.setColorized(true);
    mPlayerNotificationManager.setColor(0xFFEEEEEE);
    mPlayerNotificationManager.setUseChronometer(true);
    mPlayerNotificationManager.setOngoing(true);
    mPlayerNotificationManager.setPriority(NotificationCompat.PRIORITY_MAX);
    mPlayerNotificationManager.setUsePlayPauseActions(true);
    mPlayerNotificationManager.setSmallIcon(R.drawable.smallwidth);
    mPlayerNotificationManager.setNotificationListener(new PlayerNotificationManager.NotificationListener() {
        @Override
        public void onNotificationStarted(int notificationId, Notification notification) {
            service.startForeground(notificationId, notification);
        }

        @Override
        public void onNotificationCancelled(int notificationId) {
            service.stopSelf();
            cancelNotify();
        }
    });
    mPlayerNotificationManager.setPlayer(exoPlayer);
}

由于一些华为设备不支持MediaStyle,您需要在不设置样式的情况下生成通知。我在这款华为P8 Lite和华为Y3II上遇到过这个问题。所以,检查设备是否为上述华为和SDK版本,并创建一个简单的通知,如下所示。这个问题帮助我找到了解决办法。不管怎样,我希望能帮助别人

    boolean isLollipopHuawei = (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1 ||
            android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) && Build.MANUFACTURER.equalsIgnoreCase("HUAWEI");


    if (isLollipopHuawei) {

        builder
                .setContentTitle(description.getTitle())
                .setContentText(contentText)
                .setOngoing(true)
                .setContentIntent(createContentIntent())
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
                        R.drawable.mobi_plc))

                .addAction(R.drawable.ic_previous_outline_notification,
                        this.service.getString(R.string.next_station),
                        MediaButtonReceiver.buildMediaButtonPendingIntent(
                                this.service,
                                PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS))
                .addAction(R.drawable.ic_next_outline_notification,
                        this.service.getString(R.string.next_station),
                        MediaButtonReceiver.buildMediaButtonPendingIntent(
                                this.service,
                                PlaybackStateCompat.ACTION_SKIP_TO_NEXT))

                .setSmallIcon(R.drawable.ic_stat)

                .setAutoCancel(false);
    }

由于一些华为设备不支持MediaStyle,您需要在不设置样式的情况下生成通知。我在这款华为P8 Lite和华为Y3II上遇到过这个问题。所以,检查设备是否为上述华为和SDK版本,并创建一个简单的通知,如下所示。这个问题帮助我找到了解决办法。不管怎样,我希望能帮助别人

    boolean isLollipopHuawei = (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1 ||
            android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) && Build.MANUFACTURER.equalsIgnoreCase("HUAWEI");


    if (isLollipopHuawei) {

        builder
                .setContentTitle(description.getTitle())
                .setContentText(contentText)
                .setOngoing(true)
                .setContentIntent(createContentIntent())
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
                        R.drawable.mobi_plc))

                .addAction(R.drawable.ic_previous_outline_notification,
                        this.service.getString(R.string.next_station),
                        MediaButtonReceiver.buildMediaButtonPendingIntent(
                                this.service,
                                PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS))
                .addAction(R.drawable.ic_next_outline_notification,
                        this.service.getString(R.string.next_station),
                        MediaButtonReceiver.buildMediaButtonPendingIntent(
                                this.service,
                                PlaybackStateCompat.ACTION_SKIP_TO_NEXT))

                .setSmallIcon(R.drawable.ic_stat)

                .setAutoCancel(false);
    }

我也遇到了同样的问题,根本原因是无法设置样式,这只是在华为P8 Lite中出现了此异常,而不是在其他设备上

所以,我要做的是检查当前设备是否为android 5.0版,其制造商是否为华为,并删除setStyle属性。请检查下面的代码

boolean isLollipopHuawei = (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1 ||
            android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) && Build.MANUFACTURER.equalsIgnoreCase("HUAWEI");


if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT || Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1 || Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {

            if (isLollipopHuawei) {

                return builder
                        .addAction(generateAction(android.R.drawable.ic_media_previous, "Previous", Constants.ACTION_PREVIOUS))
                        .addAction(action)
                        .addAction(generateAction(android.R.drawable.ic_media_next, "Next", Constants.ACTION_NEXT))
                        .setSmallIcon(R.mipmap.vpicon_grayscale)
                        .setContentTitle(getSongDataHelper().getTitle())
                        .setContentIntent(pendingIntent)
                        .setContentText(getSongDataHelper().getAlbum())
                        .setLargeIcon(getSongDataHelper().getAlbumArt())
                        //.setColor(color)
                      /*  .setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                                .setShowActionsInCompactView(0, 1, 2)
                                .setMediaSession(mMediaSession.getSessionToken()))*/
                        .build();
            } else {
                return builder
                        .addAction(generateAction(android.R.drawable.ic_media_previous, "Previous", Constants.ACTION_PREVIOUS))
                        .addAction(action)
                        .addAction(generateAction(android.R.drawable.ic_media_next, "Next", Constants.ACTION_NEXT))
                        .setSmallIcon(R.mipmap.vpicon_grayscale)
                        .setContentTitle(getSongDataHelper().getTitle())
                        .setContentIntent(pendingIntent)
                        .setContentText(getSongDataHelper().getAlbum())
                        .setLargeIcon(getSongDataHelper().getAlbumArt())
                        //.setColor(color)
                        .setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                                .setShowActionsInCompactView(0, 1, 2)
                                .setMediaSession(mMediaSession.getSessionToken()))
                        .build();
            }
        }

我也遇到了同样的问题,根本原因是无法设置样式,这只是在华为P8 Lite中出现了此异常,而不是在其他设备上

所以,我要做的是检查当前设备是否为android 5.0版,其制造商是否为华为,并删除setStyle属性。请检查下面的代码

boolean isLollipopHuawei = (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1 ||
            android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) && Build.MANUFACTURER.equalsIgnoreCase("HUAWEI");


if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT || Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1 || Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {

            if (isLollipopHuawei) {

                return builder
                        .addAction(generateAction(android.R.drawable.ic_media_previous, "Previous", Constants.ACTION_PREVIOUS))
                        .addAction(action)
                        .addAction(generateAction(android.R.drawable.ic_media_next, "Next", Constants.ACTION_NEXT))
                        .setSmallIcon(R.mipmap.vpicon_grayscale)
                        .setContentTitle(getSongDataHelper().getTitle())
                        .setContentIntent(pendingIntent)
                        .setContentText(getSongDataHelper().getAlbum())
                        .setLargeIcon(getSongDataHelper().getAlbumArt())
                        //.setColor(color)
                      /*  .setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                                .setShowActionsInCompactView(0, 1, 2)
                                .setMediaSession(mMediaSession.getSessionToken()))*/
                        .build();
            } else {
                return builder
                        .addAction(generateAction(android.R.drawable.ic_media_previous, "Previous", Constants.ACTION_PREVIOUS))
                        .addAction(action)
                        .addAction(generateAction(android.R.drawable.ic_media_next, "Next", Constants.ACTION_NEXT))
                        .setSmallIcon(R.mipmap.vpicon_grayscale)
                        .setContentTitle(getSongDataHelper().getTitle())
                        .setContentIntent(pendingIntent)
                        .setContentText(getSongDataHelper().getAlbum())
                        .setLargeIcon(getSongDataHelper().getAlbumArt())
                        //.setColor(color)
                        .setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                                .setShowActionsInCompactView(0, 1, 2)
                                .setMediaSession(mMediaSession.getSessionToken()))
                        .build();
            }
        }

我不确定
PlayerNotificationManager
是否能解决这个问题;它还无条件地将
MediaStyle
传递给
setStyle()
。我不确定
PlayerNotificationManager
是否能解决这个问题;它还无条件地将
MediaStyle
传递给
setStyle()