欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android自定义通知布局错位问题

程序员文章站 2022-06-07 18:54:21
...

近期在项目中需要做一个音乐通知栏,需要自定义通知栏布局,

我在xml中编写如下布局:

Android自定义通知布局错位问题

创建通知代码如下(兼容高低版本):

private Notification buildNotification(String title, String name, String icon, Bitmap bitmap) {

     

        int playButtonResId = isPlaying() ? R.mipmap.ic_media_pause : R.mipmap.ic_media_play;

        long mNotificationPostTime = System.currentTimeMillis();

        Intent nowPlayingIntent = new Intent(this, MainActivity.class);
        nowPlayingIntent.putExtra("tab", IJumpConsts.JUMP_TAB_RING);
        nowPlayingIntent.putExtra("from", "PlayService");
        PendingIntent clickIntent = PendingIntent.getActivity(this, 0, nowPlayingIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Intent playNext = new Intent(this, PlayService.class);
        playNext.putExtra("cmd", CMD_NEXT);
        PendingIntent playNextIntent = PendingIntent.getService(this, CMD_NEXT, playNext, PendingIntent.FLAG_UPDATE_CURRENT);


        Intent playPause = new Intent(this, PlayService.class);
        playPause.putExtra("cmd", CMD_PAUSE);
        PendingIntent playPauseIntent = PendingIntent.getService(this, CMD_PAUSE, playPause, PendingIntent.FLAG_UPDATE_CURRENT);


        Intent playPre = new Intent(this, PlayService.class);
        playPre.putExtra("cmd", CMD_PRE);
        PendingIntent playPreIntent = PendingIntent.getService(this, CMD_PRE, playPre, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.view_music_play_remote_view);

        contentView.setTextViewText(R.id.title,title);
        contentView.setTextViewText(R.id.name,name);
        contentView.setImageViewResource(R.id.play,playButtonResId);
        contentView.setImageViewBitmap(R.id.icon,bitmap);

        contentView.setOnClickPendingIntent(R.id.play,playPauseIntent);
        contentView.setOnClickPendingIntent(R.id.pre,playPreIntent);
        contentView.setOnClickPendingIntent(R.id.next,playNextIntent);

        Notification.Builder builder = null;
        builder = new Notification.Builder(this)
                .setDefaults(NotificationCompat.FLAG_ONLY_ALERT_ONCE)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(clickIntent)
                .setContentTitle(title)
                .setContentText(name)
                .setSound(null)
                .setWhen(mNotificationPostTime);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            builder.setCustomContentView(contentView);
        }else {
            builder.setContent(contentView);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelId = "music_play";
            String channelName = "music_play";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
            // 设置通知出现时不震动
            channel.enableVibration(false);
            channel.enableLights(false);
            channel.setVibrationPattern(new long[]{0});
            channel.setSound(null, null);
            notificationManager.createNotificationChannel(channel);
            builder.setChannelId(channelId);
        }

        builder.setShowWhen(false);


        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            builder.setVisibility(Notification.VISIBILITY_PUBLIC);
            Notification.MediaStyle style = new Notification.MediaStyle()
                    .setMediaSession(mSession.getSessionToken());
            builder.setStyle(style);
        }

        return builder.build();
    }

需要注意的地方是setContent和setCustomContentView ,因为后者只能在android7以上使用,因此做了兼容。

在android7以上的机子运行效果如下:

Android自定义通知布局错位问题

界面是正常的,没有任何问题,但是测试发现在android5~6,会出现按钮不见,或者布局异常如下图:

Android自定义通知布局错位问题

Android自定义通知布局错位问题

不仅按钮不见,而且会出现一条横线。有的机型是没有横线但是按钮不见了无法控制播放暂停(按钮是用ImageView实现,remoteViews是支持的)。

 

开始是怀疑布局的问题,但是最终发现不是。

 

因为一开始想用系统的默认布局所以设置了Style,也就是以下的代码。既然自定义的话,也就没有必要设置以下代码了,去掉之后就正常了。


        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            builder.setVisibility(Notification.VISIBILITY_PUBLIC);
            Notification.MediaStyle style = new Notification.MediaStyle()
                    .setMediaSession(mSession.getSessionToken());
            builder.setStyle(style);
        }

修改后高版本正常,安卓5~6运行效果如下:

Android自定义通知布局错位问题

 

暂时如此解决,具体原因等我有空看看源代码在补充。