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


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:background="@color/play_main_dark_bg"
    android:id="@+id/notice"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/widget_album"
        android:layout_width="64dp"
        android:layout_height="match_parent"
        android:padding="5dp"
        android:contentDescription="这是专辑图片" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="32dp"
            android:orientation="vertical"
            android:paddingLeft="9dp"
            android:paddingRight="9dp"
            >

                <TextView
                    android:id="@+id/widget_title"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:ellipsize="end"
                    android:singleLine="true"
                    android:gravity="center_vertical"
                    android:textColor="#ffffff"
                    android:textSize="12sp" />
                <TextView
                    android:id="@+id/widget_artist"
                    android:layout_width="wrap_content"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:ellipsize="end"
                    android:singleLine="true"
                    android:textColor="#A0A0A0"
                    android:gravity="center_vertical"
                    android:textSize="10sp" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="32dp"
            android:gravity="center"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/widget_prev"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="4dp"
                android:src="@drawable/audioplayer_play_pre_white" />

            <ImageView
                android:id="@+id/widget_play"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/audioplayer_play_pause_white"
                />

            <ImageView
                android:id="@+id/widget_next"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="4dp"
                android:src="@drawable/audioplayer_play_next_white" />

        </LinearLayout>
    </LinearLayout>

</LinearLayout>

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

白色RemoteViews layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:background="#FFF8F8F8"
    android:id="@+id/notice"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/widget_album"
        android:layout_width="64dp"
        android:layout_height="match_parent"
        android:padding="5dp"
        android:contentDescription="这是专辑图片" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="32dp"
            android:orientation="vertical"
            android:paddingLeft="9dp"
            android:paddingRight="9dp"
            >

                <TextView
                    android:id="@+id/widget_title"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:ellipsize="end"
                    android:singleLine="true"
                    android:gravity="center_vertical"
                    android:textColor="@color/black"
                    android:textSize="12sp" />
                <TextView
                    android:id="@+id/widget_artist"
                    android:layout_width="wrap_content"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:ellipsize="end"
                    android:singleLine="true"
                    android:textColor="@color/gray"
                    android:gravity="center_vertical"
                    android:textSize="10sp" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="32dp"
            android:gravity="center"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/widget_prev"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="4dp"
                android:src="@drawable/audioplayer_play_pre_black" />

            <ImageView
                android:id="@+id/widget_play"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/audioplayer_play_pause_black"
                />

            <ImageView
                android:id="@+id/widget_next"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:padding="4dp"
                android:layout_weight="1"
                android:src="@drawable/audioplayer_play_next_black" />

        </LinearLayout>
    </LinearLayout>

</LinearLayout>

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

白色效果如图:

这里我的需求是音频类的通知栏所以自定义的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);
        }
    }

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

更多文章请关注公众号:

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值