复现问题,就是在没有进入音乐播发器之前就去添加音乐小部件play all,然后点击播放,就会报错。
报错信息:
01-08 13:48:56.116380 6294 6294 E AndroidRuntime: FATAL EXCEPTION: main
01-08 13:48:56.116380 6294 6294 E AndroidRuntime: Process: com.android.music, PID: 6294
01-08 13:48:56.116380 6294 6294 E AndroidRuntime: android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=music_notification_channel pri=0 contentView=com.android.music/0x7f04000f vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 vis=PRIVATE)
01-08 13:48:56.116380 6294 6294 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1765)
01-08 13:48:56.116380 6294 6294 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
01-08 13:48:56.116380 6294 6294 E AndroidRuntime: at android.os.Looper.loop(Looper.java:193)
01-08 13:48:56.116380 6294 6294 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6702)
01-08 13:48:56.116380 6294 6294 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
01-08 13:48:56.116380 6294 6294 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
01-08 13:48:56.116380 6294 6294 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)
通过log可以看出是channel问题,查看源码发现,它只是定义了channel=music_notification_channel字符串,其他的没有做,没有发现定义NotificationChannel,所以自己定义一个,发现问题解决。
Notification status = new Notification.Builder
(context, MusicBrowserActivity.MUSIC_NOTIFICATION_CHANNEL).build();
修改文件:
alps/vendor/mediatek/proprietary/packages/apps/Music/src/com/android/music/MediaPlaybackService.java
import com.mediatek.bluetooth.avrcp.ServiceAvrcpStub;
/// BT AVRCP End @}
//Redmine161113 modified for Music player error 2019/1/16 begin
import android.app.NotificationManager;
import android.app.NotificationChannel;
//Redmine161113 modified for Music player error 2019/1/16 end
private boolean mTaskRemoved = false;
private boolean misAVRCPseeking = false;
private boolean mPlayBeforeseek = false;
//Redmine161113 modified for Music player error 2019/1/16 begin
public static final String MUSIC_NOTIFICATION_CHANNEL = "music_notification_channel";
//Redmine161113 modified for Music player error 2019/1/16 end
private void updateNotification(Context context, Bitmap bitmap) {
.......
intent = new Intent("com.android.music.PLAYBACK_VIEWER");
/// M: Create NotificationChannel
//Notification status = new Notification();
//Redmine161113 modified for Music player error 2019/1/16 begin
/// M: Create NotificationChannel
NotificationManager mgr = (NotificationManager)this.
getSystemService(Context.NOTIFICATION_SERVICE);
mgr.createNotificationChannel(new NotificationChannel(
MUSIC_NOTIFICATION_CHANNEL, "MUSIC", NotificationManager.IMPORTANCE_LOW));
Notification status = new Notification.Builder
(context, MUSIC_NOTIFICATION_CHANNEL).build();
//Redmine161113 modified for Music player error 2019/1/16 end
status.contentView = views;
status.flags |= Notification.FLAG_ONGOING_EVENT;
status.icon = R.drawable.stat_notify_musicplayer;
status.contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
mMediaplayerHandler.removeMessages(DEALY_STOPFORGROUND);
startForeground(PLAYBACKSERVICE_STATUS, status);
}
问题解决,如有问题,可以留言,大家一起学习,谢谢!