Android 自定义通知栏适配

之前做音乐播放器的需求中有要通知栏部分,发现不同手机显示的系统通知栏千奇百怪不统一,需要适配。分析发现目前大部分手机通知栏不是白色就是深色,因此经过考虑定义两种不同的布局,根据不同通知栏背景颜色来创建不同的RemoteViews。我们可以自定义Notification,并且可以设置contentView以及bigContentView从而达到我们想要的布局以及背景颜色。

RemoteViews仅支持FrameLayout、LinearLayout、RelativeLayout三种布局控件和AnalogClock、Chronometer、Button、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView和AdapterViewFlipper这些显示控件,不支持这些类的子类或Android提供的其他控件,否则会引起异常。

下面从我的需求实现来实现两种布局:

深色RemoteViews layout.xml





    

    

        

                
                

        

        

            

            

            

        
    

布局的高度最高应该不超过256dp,但是有些手机锁屏显示不全,因此我限制在64dp,基本上不会有锁屏显示不全问题。

白色RemoteViews layout.xml




    

    

        

                
                

        

        

            

            

            

        
    

基本布局只改变了背景颜色和图片。

白色效果如图:

Android 自定义通知栏适配_第1张图片

这里我的需求是音频类的通知栏所以自定义的notication的style为MediaStyle


notificationBuilder
                .setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
//                        .setShowActionsInCompactView(
//                                new int[]{0, 1, 2})
                        .setMediaSession(mSessionToken));

看一下自定义的Notification:

public Notification createNotification() {
        Log.d(TAG, "createNotification. mMediaMetadata=" + mMediaMetadata);
        // 创建通知栏
        if (mMediaMetadata == null || mPlaybackState == null) {
            return null;
        }


        // Notification channels are only supported on Android O+.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel();//Android O以上必须要创建NotificationChannel
        }


        final NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(mMusicService, CHANNEL_ID);
        RemoteViews normalView;
        if(isDarkNotificationTheme){//判断是否使用深色通知栏
            normalView = new RemoteViews(mMusicService.getPackageName(), R.layout.normal_notification);
        }else{
            normalView = new RemoteViews(mMusicService.getPackageName(), R.layout.normal_notification_white);
        }
        normalView.setOnClickPendingIntent(R.id.widget_prev, mPreviousIntent);
        normalView.setOnClickPendingIntent(R.id.widget_next, mNextIntent);
        if (mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING) {
            if(NotifiationColorUtil.isDarkNotificationTheme(mMusicService)){
                normalView.setOnClickPendingIntent(R.id.widget_play, mPauseIntent);
                normalView.setImageViewResource(R.id.widget_play,R.drawable.audioplayer_play_playing_white);
            }else{
                normalView.setOnClickPendingIntent(R.id.widget_play, mPauseIntent);
                normalView.setImageViewResource(R.id.widget_play,R.drawable.audioplayer_play_playing_black);
            }   
        } else {
            if(NotifiationColorUtil.isDarkNotificationTheme(mMusicService)){
                normalView.setOnClickPendingIntent(R.id.widget_play, mPlayIntent);
                normalView.setImageViewResource(R.id.widget_play,R.drawable.audioplayer_play_pause_white);
            }else{
                normalView.setOnClickPendingIntent(R.id.widget_play, mPlayIntent);
                normalView.setImageViewResource(R.id.widget_play,R.drawable.audioplayer_play_pause_black);
            }
        }
        MediaDescriptionCompat description = mMediaMetadata.getDescription();
        normalView.setTextViewText(R.id.widget_title,getAppName(mMusicService));
        normalView.setTextViewText(R.id.widget_artist,description.getTitle());
        Bitmap art = null;
        if (description.getIconUri() != null) {
            // This sample assumes the iconUri will be a valid URL formatted String, but
            // it can actually be any valid Android Uri formatted String.
            // async fetch the album art icon
            String artUrl = description.getIconUri().toString();
            art = AlbumArtCache.getInstance().getBigImage(artUrl);
            if (art == null) {
                // use a placeholder art while the remote art is being downloaded
                art = BitmapFactory.decodeResource(mMusicService.getResources(),
                        R.drawable.audioplayer_ic_notification);
            }
        }

        notificationBuilder
                .setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
//                        .setShowActionsInCompactView(
//                                new int[]{0, 1, 2})
                        .setMediaSession(mSessionToken))
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setColor(mNotificationColor)
                .setSmallIcon(R.drawable.audioplayer_ic_notification)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setContentIntent(createContentIntent(description));
        normalView.setImageViewBitmap(R.id.widget_album,art);
        setNotificationPlaybackState(notificationBuilder);
        Notification notification=notificationBuilder.build();
        notification.contentView=normalView;// 将自定义的view设置给notification
        notification.bigContentView=normalView;//将自定义的view设置给notification
        return notification;
    }
@RequiresApi(Build.VERSION_CODES.O)
    public void createNotificationChannel() {
        if (mNotificationManager.getNotificationChannel(CHANNEL_ID) == null) {
            NotificationChannel notificationChannel =
                    new NotificationChannel(CHANNEL_ID,
                            "YOUR_Channel_ID",
                            NotificationManager.IMPORTANCE_LOW);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
            notificationChannel.setDescription("Channel ID for YOU");

            mNotificationManager.createNotificationChannel(notificationChannel);
        }
    }

通过以上基本能适配大部分手机通知栏。

更多文章请关注公众号:

Android 自定义通知栏适配_第2张图片

 

你可能感兴趣的:(android)