安卓仿网易云音乐通知栏控制音乐,默认显示Notification bigView
来源:互联网 发布:如何应对网络喷子 编辑:程序博客网 时间:2024/06/05 11:31
最近在做一个音乐播放器的时候遇到了一个关于notification的问题,在网上找了很久都没有头绪。后来找到了解决的办法,特意记录一下。
问题描述
首先请看网易云音乐的通知栏
可以看到,网易云音乐的通知栏有两种,一种是普通的通知栏,一种是显示bigView的通知栏。这两种通知栏是可以通过双指上滑/下滑来切换的(说实话,我今天才知道原来还有这个功能…)。
网易云音乐在播放音乐的时候,默认显示的是第二种通知栏,而我今天遇到的问题是:通过自定义布局来实现通知栏,默认情况下只能显示第一种普通高度的通知栏,需要双指滑动一下才能显示bigView通知栏。
对于大多数用户来说,可能并不知道有双指滑动通知栏这一功能,因此这样的用户体验是不够友好的,那么,如何实现像网易云一样的,默认就显示bigView的notification呢?
首先新建一个测试项目,再新建两个布局,一个用作普通的notification视图,一个用作bigView的notification视图:
- 普通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设置最高的优先级
设置了优先级之后,终于能够实现和网易云音乐相同的通知栏效果了,大功告成,下面上图:
默认启动的时候:
双指滑动后效果:
PS:
1.上面写的两个布局文件没有参考意义,快下班了,噼里啪啦写出来的,估计有些问题
2.有空录下GIF,效果应该会更直观;
3.如果看不到图了,请到http://www.jianshu.com/p/77da5abd3870查看
- 安卓仿网易云音乐通知栏控制音乐,默认显示Notification bigView
- Android开发:用通知栏(Notification Bar)控制音乐的后台播放
- 音乐播放时在通知栏显示大小不同的 notification
- 网易云音乐snap
- 网易云音乐爬虫
- 安装网易云音乐
- 仿网易云音乐
- 网易云 音乐下载
- 网易云音乐爬虫
- 仿网易云音乐播放音乐转盘
- Logitech的SetPointP控制网易云音乐,千千静听
- Android音乐播放器---实现Notification控制音乐播放
- Android音乐播放器---实现Notification控制音乐播放
- Android音乐播放器---实现Notification控制音乐播放
- Android音乐播放器---实现Notification控制音乐播放
- 网易云音乐使用中显示"请切换输出设备"
- Android 4.1 Music 通知栏的音乐控制
- Android 4.1 Music 通知栏的音乐控制
- 二维数组转换
- python3 运算符
- LeetCode 326. Power of Three (Easy)
- 嵌入式arm210开发板移植Apache的详细步骤
- Eclipse离线安装TestNG插件
- 安卓仿网易云音乐通知栏控制音乐,默认显示Notification bigView
- SharedPreferences
- 二进制 八进制 十进制 十六进制的转换
- NOIP2017_DAY1
- Zephyr OS之源码树结构
- 动态规划 java 矩阵连乘
- 初步了解Tomcat服务器(二)
- NodeJs学习 -- 异步机制
- 数组循环右移问题