安卓仿网易云音乐通知栏控制音乐,默认显示Notification bigView

最近在做一个音乐播放器的时候遇到了一个关于notification的问题,在网上找了很久都没有头绪。后来找到了解决的办法,特意记录一下。

问题描述

首先请看网易云音乐的通知栏
普通高度的notification.png
加高notification.png

可以看到,网易云音乐的通知栏有两种,一种是普通的通知栏,一种是显示bigView的通知栏。这两种通知栏是可以通过双指上滑/下滑来切换的(说实话,我今天才知道原来还有这个功能…)。

网易云音乐在播放音乐的时候,默认显示的是第二种通知栏,而我今天遇到的问题是:通过自定义布局来实现通知栏,默认情况下只能显示第一种普通高度的通知栏,需要双指滑动一下才能显示bigView通知栏。

对于大多数用户来说,可能并不知道有双指滑动通知栏这一功能,因此这样的用户体验是不够友好的,那么,如何实现像网易云一样的,默认就显示bigView的notification呢?

首先新建一个测试项目,再新建两个布局,一个用作普通的notification视图,一个用作bigView的notification视图:

  1. 普通notification视图,命名为normal_notification.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="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:src="@mipmap/icon"/>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="64dp"
        android:layout_marginEnd="10dp"
        android:layout_marginStart="10dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="歌曲名"
            android:textSize="18sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="歌手名"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="64dp"
        android:gravity="center_vertical">
        <ImageButton
            android:layout_width="26dp"
            android:layout_height="26dp"
            android:background="@null"
            android:scaleType="fitCenter"
            android:src="@mipmap/pre"/>

        <ImageButton
            android:layout_marginStart="10dp"
            android:layout_width="26dp"
            android:layout_height="26dp"
            android:background="@null"
            android:scaleType="fitCenter"
            android:src="@mipmap/play"/>

        <ImageButton
            android:layout_marginStart="10dp"
            android:layout_width="26dp"
            android:layout_height="26dp"
            android:background="@null"
            android:scaleType="fitCenter"
            android:src="@mipmap/next"/>
    </LinearLayout>

</LinearLayout>

2.bigView的notification视图,命名为big_notification.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="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="110dp"
        android:layout_height="110dp"
        android:src="@mipmap/icon"/>

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

        <TextView
            android:id="@+id/tv_song_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="10dp"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:text="歌名"
            android:textSize="15sp"/>

        <TextView
            android:id="@+id/tv_song_singer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="10dp"
            android:layout_marginStart="10dp"
            android:layout_marginTop="5dp"
            android:text="歌名"
            android:textSize="12sp"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="20dp"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <ImageButton
                android:layout_marginEnd="10dp"
                android:layout_width="26dp"
                android:layout_height="26dp"
                android:background="@null"
                android:scaleType="fitCenter"
                android:src="@mipmap/like"/>

            <ImageButton
                android:layout_marginEnd="10dp"
                android:layout_width="26dp"
                android:layout_height="26dp"
                android:background="@null"
                android:scaleType="fitCenter"
                android:src="@mipmap/pre"/>

            <ImageButton
                android:layout_marginEnd="10dp"
                android:layout_width="38dp"
                android:layout_height="38dp"
                android:background="@null"
                android:scaleType="fitCenter"
                android:src="@mipmap/play"/>

            <ImageButton
                android:layout_marginEnd="10dp"
                android:layout_width="26dp"
                android:layout_height="26dp"
                android:background="@null"
                android:scaleType="fitCenter"
                android:src="@mipmap/next"/>

        </LinearLayout>
    </LinearLayout>
</LinearLayout>

除了这两个用作notification的布局文件之外,在主界面中还有一个按钮,通过按钮的点击事件,模拟开始播放音乐,发送通知。这里就不贴代码了,直接贴按钮点击事件中的核心代码:

//发送自定义视图通知
public void sendCustomViewNotification(View view) {
    //普通notification用到的视图
    RemoteViews normalView = new RemoteViews(getPackageName(), R.layout.normal_notification);
    //显示bigView的notification用到的视图
    RemoteViews bigView = new RemoteViews(getPackageName(), R.layout.big_notification);

    Notification notification = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setTicker("开始播放啦~~")
            .setOngoing(true)
            .setContent(normalView)//设置普通notification视图
            .setCustomBigContentView(bigView)//设置显示bigView的notification视图  
            .setPriority(NotificationCompat.PRIORITY_MAX)//设置最大优先级
            .build();

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(22, notification);
    }

上面这段代码相信大家应该经常使用,这是构建一个自定义通知的基本代码。在使用builder对象为notification设置一些列熟悉的时候,有三个关键的方法,我都已经注释出来了

1.setContent:给notification设置普通状态下显示的视图
2. setCustomBigContentView():给notification设置bigView

再设置完上面两个方法之后,程序应该能实现双指滑动切换普通notification和bigView notification了,但还无法像网易云的通知栏一样,默认显示bigView notification,所以:

    3. setPriority(NotificationCompat.PRIORITY_MAX):给notification设置最高的优先级

设置了优先级之后,终于能够实现和网易云音乐相同的通知栏效果了,大功告成,下面上图:

默认启动的时候:

默认启动时显示的通知栏

双指滑动后效果:

2.png

PS:
1.上面写的两个布局文件没有参考意义,快下班了,噼里啪啦写出来的,估计有些问题
2.有空录下GIF,效果应该会更直观;
3.如果看不到图了,请到http://www.jianshu.com/p/77da5abd3870查看

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值