通知栏Notification的使用
在这一片博客中,小达将自己学习Notification的一些想法和大家分享一哈,学的不是很深,所有有些东西可能解释的不是特别到位,还请各位谅解哈
.所谓Notification即
通知,是一种让你的应用程序在不使用Activity的情况下警示用户。它是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)
警示用户有需要注意的事件发生的最好途径。后面的一大段一大段是接着前面的项目在继续,大家可以选择性的看一看.

Notification 是由NotificationManager(系统服务)统一管理的。挂在下拉通知栏上面,就像下图所示,
上面显示的都是所谓的Notification,今天我们就来一步步的创建,添加按钮响应,再将Notification和我们的播放器联系起来,话不多说,现在就开始咯.
先给Notification创建了个单独的布局文件,当notification被激活的时候,就在通知栏上面显示这个布局文件的样式,
notification_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/notification_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_photo_01">
<ImageView //显示歌手头像的,,,,,基本没怎么用上,专辑图片不知道怎么弄得出不来
android:id="@+id/notification_artist_image"
android:layout_width="64.0dip"
android:layout_height="64.0dip"
android:background="#00000000"/>
<TextView //在通知栏上显示音乐名称的TextView
android:id="@+id/notification_music_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginStart="5.0dp"
android:layout_toRightOf="@+id/notification_artist_image"
android:layout_toEndOf="@+id/notification_artist_image"
android:focusable="true"
android:textColor="#FFADABFF"
android:textSize="18sp"
/>
<TextView<span style="font-family: Arial, Helvetica, sans-serif;"> //在通知栏上显示歌手的TextView</span>
android:id="@+id/notification_music_Artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/notification_music_title"
android:layout_alignStart="@+id/notification_music_title"
android:layout_marginTop="5.0dip"
android:layout_below="@id/notification_music_title"
android:textColor="#FFADABFF"
/>
<ImageButton //通知栏上的退出按钮
android:id="@+id/notification_exit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/notification_exit"
android:layout_below="@+id/notification_music_title"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageButton
android:id="@+id/notification_next_song_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/next_music_photo"
android:layout_toLeftOf="@+id/notification_exit_button"
android:layout_toStartOf="@+id/notification_exit_button"
/>
<ImageButton
android:id="@+id/notification_play_button"
android:layout_toLeftOf="@id/notification_next_song_button"
android:layout_toStartOf="@id/notification_next_song_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"/>
<ImageButton
android:id="@+id/notification_previous_song_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/notification_play_button"
android:layout_toStartOf="@id/notification_play_button"
android:background="@drawable/previous_music_photo"
/>
</RelativeLayout>
布局文件大致弄好了后,就在播放歌曲的同时,直接将这个布局文件显示到通知栏上去,这一步需要Notification和NotificationManager,小达这里是将notification放在了service里面进行操作了,其他地方也可以的,这是service里面的一部分代码,后面会有整个service的代码的,:
private void initMyNotification() {
/*
第一个参数是显示在通知栏上的小图标的图片
第二个参数是在启动notification时,在状态栏上滚动的一句话
第三个参数是在状态栏上显示的时间
*/
myNotification = new Notification(R.drawable.notification_artist_default_image, "小卷毛音乐", System.currentTimeMillis());
/*
NotificationManager用来管理notification的显示和消失等
*/
myNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/*
对刚才创建的一个notification实例设置各种参数
其中的一个参数是flags
这个参数可以设置该notification是何种状态
这里设置的是ONGOING,表示这个notification会一直呆在通知栏上面
也可以设置滑动后就消失的样式,看个人的不同需求
*/
myNotification.flags = N