通知栏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="w