Android Oreo 通知崩溃系统 UI [英] Android Oreo Notification Crashes System UI

查看:30
本文介绍了Android Oreo 通知崩溃系统 UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法让通知在较旧的 API 中起作用,但在 Oreo 中不起作用.创建通知会导致我的应用程序仍然正常工作(logcat 中没有消息),但是 SystemUI 崩溃并在 Activity 运行时无限循环重新启动.systemui进程的logcat报错:

I've managed to get notifications working in older API's, but not Oreo. Creating the notification causes my app to still work fine (no messages in logcat), however SystemUI crashes and reboots in an endless cycle while the Activity is running. The is the error in logcat for the systemui process:

java.lang.IllegalArgumentException: width and height must be > 0

我的代码:

private void showPlayingNotification() {
        NotificationCompat.Builder builder = mNotificationUtils.getAndroidChannelNotification(this, "Play", mMediaSessionCompat);
        if( builder == null ) {
            Log.i("Play Notification","No notification found!");
            return;
        }

        mNotificationUtils.getManager().notify(101,builder.build()); 
}

我在创建的 MediaPlayerService 的 onCreate 中初始化了 mNotificationUtils.

I initialised mNotificationUtils in the onCreate of the MediaPlayerService I created.

public class NotificationUtils extends ContextWrapper {

    private NotificationManager mManager;
    public static final String AUDIO_CHANNEL_ID = "com.liftyourheads.dailyreadings.dailyReadingsAudio";
    public static final String AUDIO_CHANNEL_NAME = "Daily Readings Audio Stream";

    public NotificationUtils(Context base) {
        super(base);
        createChannels();
    }

    public void createChannels() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // create android channel
            NotificationChannel dailyReadingsAudioChannel = new NotificationChannel(AUDIO_CHANNEL_ID,
                    AUDIO_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            getManager().createNotificationChannel(dailyReadingsAudioChannel);

        }
    }

    public NotificationManager getManager() {
        if (mManager == null) {
            mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return mManager;
    }

    public NotificationCompat.Builder getAndroidChannelNotification(Context context, String action, MediaSessionCompat mediaSession) {

        if (action.equals("Play")) {
            return MediaStyleHelper.from(context, mediaSession)
                    .addAction(new NotificationCompat.Action(android.R.drawable.ic_media_pause, "Pause", MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY_PAUSE)))
                    .setStyle(
                            new android.support.v4.media.app.NotificationCompat.MediaStyle()
                                    .setShowActionsInCompactView(0)
                                    .setMediaSession(mediaSession.getSessionToken()))
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentText("Content Text")
                    .setContentTitle("Content Title")
                    .setChannelId(AUDIO_CHANNEL_ID);

        } else if (action.equals("Pause")) {

            return MediaStyleHelper.from(context, mediaSession)
                    .addAction(new NotificationCompat.Action(android.R.drawable.ic_media_play, "Play", MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY_PAUSE)))
                    .setStyle(
                            new android.support.v4.media.app.NotificationCompat.MediaStyle()
                                    .setShowActionsInCompactView(0)
                                    .setMediaSession(mediaSession.getSessionToken()))
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentText("Content Text")
                    .setContentTitle("Content Title")
                    .setChannelId(AUDIO_CHANNEL_ID);

        }

        return null;

    } }

推荐答案

将图标从 mipmap 切换到 drawable.有关详情,请参阅此问题.

Switch from mipmap to drawable for the icon. See this issue for more.

这篇关于Android Oreo 通知崩溃系统 UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆