安卓8.0通知导致系统界面崩溃

15

我已经成功在旧的 API 中使通知工作,但在 Oreo 中无法实现。创建通知会导致我的应用程序仍然正常工作(logcat 中没有消息),但是 SystemUI 在 Activity 运行时崩溃并重新启动,形成无限循环。这是系统 UI 进程中的错误:

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。

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;

    } }
3个回答

29

将图标从 mipmap 切换到 drawable。有关更多信息,请参见此问题


谢谢!这就是原因。 - Jake Madden
我将mipmap更改为drawable,但仍然遇到相同的问题。 - Adil
mipmap 在大多数三星设备上运行正常,但在我的 HTC U11 上崩溃了。使用 drawable 解决了这个问题。@Andrea Scalabrini 的答案提供了更多有关此问题的信息。 - CodeRed
如果我们将它从mipmap更改为drawable,那么如何为通知图标drawable放置一个常量资源ID。Gradle的最近版本忽略了public.xml文件。 - shivam gupta
@CommonsWare,抱歉,请允许我解释一下这个问题。如果我把通知图标放在drawable文件夹下面,那么生成的资源ID就可以由旧版本构建更改为新版本构建,因为任何开发人员都可以引入新的可绘制对象。由于可绘制资源ID发生了变化,有时会在应用程序更新时出现RemoteServiceException异常。这就是为什么我需要一种方法来获得一个常量资源ID,以便从一个构建到另一个构建时不会发生变化。 - shivam gupta
显示剩余2条评论

8
问题与Android O中的新自适应图标有关。
为解决此问题,请将所有自适应图标替换为经典图标,无论是mipmap还是drawable。
一些参考资料: 链接1 链接2

5

当我尝试创建通知时,我的应用程序崩溃了。

就我的情况而言,我使用了一个名为“基本活动”的Android Studio示例项目,其中包含下面的AndroidManifest.xml文件。mipmap/ic_launcher和mipmap/ic_launcher_round用作应用程序图标。

<application
    android:name=".DriveMeApp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

这个项目包含了上方的ic_launcher.xmlic_launcher_round.xml自适应图标。为了解决我的崩溃问题,我必须删除这两个文件。删除了自适应图标文件后,应该使用png文件作为应用程序图标。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接