Android Oreo 通知导致系统崩溃 UI
Android Oreo Notification Crashes System UI
我已经设法让旧 API 的通知正常工作,但奥利奥不行。创建通知会导致我的应用程序仍然正常工作(logcat 中没有消息),但是当 Activity 为 运行 时,SystemUI 崩溃并无限循环重启。这是 systemui 进程 logcat 中的错误:
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;
} }
将图标从 mipmap
切换到 drawable
。有关更多信息,请参阅 this issue。
此问题与 Android O 中的新自适应图标有关。
解决办法,将自适应图标全部换成经典图标即可。
无论是 mipmap 还是 drawable
当我尝试创建通知时,我的应用程序崩溃了。
就我而言,我使用的是 Android Studio 示例项目 "Basic Activity",其中包含下面的 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.xml和ic_launcher_round.xml自适应图标。我不得不删除这两个文件来解决我的崩溃问题。删除自适应图标文件后,应用程序图标应为png文件
我已经设法让旧 API 的通知正常工作,但奥利奥不行。创建通知会导致我的应用程序仍然正常工作(logcat 中没有消息),但是当 Activity 为 运行 时,SystemUI 崩溃并无限循环重启。这是 systemui 进程 logcat 中的错误:
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;
} }
将图标从 mipmap
切换到 drawable
。有关更多信息,请参阅 this issue。
此问题与 Android O 中的新自适应图标有关。
解决办法,将自适应图标全部换成经典图标即可。 无论是 mipmap 还是 drawable
当我尝试创建通知时,我的应用程序崩溃了。 就我而言,我使用的是 Android Studio 示例项目 "Basic Activity",其中包含下面的 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.xml和ic_launcher_round.xml自适应图标。我不得不删除这两个文件来解决我的崩溃问题。删除自适应图标文件后,应用程序图标应为png文件