1.显示普通通知栏。新建一个 module,在界面上写两个按钮,一个显示通知,一个隐藏通知
MainActivity.java
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String msg=getIntent().getStringExtra("msg");
if (msg!=null){
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
}
public void show(View view){
showNotification();
}
/** 显示通知栏*/
private void showNotification() {
Notification notification=new Notification(R.drawable.icon,"正在播放:
dida",System.currentTimeMillis());
notification.setLatestEventInfo(this, "温柔", "五月天", getPendingIntent());
//不允许删除通知栏
notification.flags=Notification.FLAG_ONGOING_EVENT;
NotificationManager notificationManger= (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
notificationManger.notify(0,notification);
}
private PendingIntent getPendingIntent() {
Intent intent=new Intent(this,MainActivity.class);
intent.putExtra("msg","notification 发出的通知");
return PendingIntent.getActivity(this,0,
intent,PendingIntent.FLAG_UPDATE_CURRENT);
}
public void cancel(View view){
cancelNotification();
}
/** 取消通知栏*/
private void cancelNotification() {
NotificationManager notificationManger= (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
notificationManger.cancel(0);
}
}
2.使用新的 API 显示通知栏
/** 使用新的 api*/
2. private Notification getNewNotification() {
3. Notification.Builder notification=new Notification.Builder(this);
notification.setSmallIcon(R.drawable.icon);
notification.setTicker("正在播放:温柔");
notification.setOngoing(true);
notification.setContentTitle("温柔");
notification.setContentText("五月天");
notification.setWhen(System.currentTimeMillis());
notification.setContentIntent(getPendingIntent());
return notification.getNotification();
}
3.自定义通知栏。写一个通知栏的布局,分别填充上面的文字和点击事件
notification.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dp"
android:id="@+id/audioplay_ll_notification"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/audioplay_tv_notification_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="歌曲名"
android:textColor="@color/white"
android:textSize="18sp"/>
<TextView
android:id="@+id/audioplay_tv_notification_artist"
android:layout_marginTop="4dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="歌手名"
android:textColor="@color/halfwhite"
android:textSize="16sp"/>
</LinearLayout>
<ImageView
android:id="@+id/audioplay_iv_notification_pre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="@drawable/icon_notification_pre"/>
<ImageView
android:id="@+id/audioplay_iv_notification_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_notification_next"/>
</LinearLayout>
自定义通知栏代码的实现
/** 使用新的 API 自定义一个通知*/
private Notification getCustomNotification() {
Notification.Builder notification=new Notification.Builder(this);
notification.setSmallIcon(R.drawable.icon);
notification.setTicker("正在播放:我相信");
notification.setOngoing(true);
// notification.setWhen(System.currentTimeMillis());
// notification.setContentTitle("北京北京");
// notification.setContentText("汪峰");
// notification.setContentIntent(getPendingIntent());、
notification.setContent(getRemoteView());
return notification.getNotification();
}
/** 自定义通知栏显示的布局*/
private RemoteViews getRemoteView() {
RemoteViews remoteViews=new
RemoteViews(getPackageName(),R.layout.notification);
//填充文字
remoteViews.setTextViewText(R.id.audioplay_tv_notification_name,"我相信");
remoteViews.setTextViewText(R.id.audioplay_tv_notification_artist,"unknow");
//设置点击事件
remoteViews.setOnClickPendingIntent(R.id.audioplay_ll_notification,
getPendingIntent());
remoteViews.setOnClickPendingIntent(R.id.audioplay_iv_notification_pre,
getPrePendingIntent());
remoteViews.setOnClickPendingIntent(R.id.audioplay_iv_notification_next,
getNextPendingIntent());
return remoteViews;
}
/** 下一首*/
private PendingIntent getNextPendingIntent() {
Intent intent=new Intent(this,MainActivity.class);
intent.putExtra("msg", "下一首");
return PendingIntent.getActivity(this,2,
intent,PendingIntent.FLAG_UPDATE_CURRENT);
}
/** 上一首*/
private PendingIntent getPrePendingIntent() {
Intent intent=new Intent(this,MainActivity.class);
intent.putExtra("msg", "上一首");
//注意有多个 PendingIntent 的时候第二个参数不能一样
return PendingIntent.getActivity(this,1,
intent,PendingIntent.FLAG_UPDATE_CURRENT);
}
private PendingIntent getPendingIntent() {
Intent intent=new Intent(this,MainActivity.class);
intent.putExtra("msg", "notification 发出的通知");
return PendingIntent.getActivity(this,0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
}
4.将自定义的通知集成到项目中去。将布局文件和代码直接拷贝到项目中,代码需要放在服务中。最后将歌名和歌手名的数据初始化一下
/** 音乐暂停*/
public void pause(){
mediaPlayer.pause();
cancelNotification();
}
/** 音乐开始*/
public void start(){
mediaPlayer.start();
showNotification();
}
private class OnAudioPreparedListener implements MediaPlayer.OnPreparedListener {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
//准备完成,开始播放
mediaPlayer.start();
//服务发广播来更新界面的图片
Intent intent=new Intent("com.itheima.mediaplay.updateplaybtn");
//获取当前正在播放的音乐
AudioItem audioItem=audioItems.get(position);
intent.putExtra("audioItem",audioItem);
sendBroadcast(intent);
//显示通知
showNotification();
}
}
5.通知栏中上一首和下一首功能的实现。 在 onStartCommand 中先去判断点击是不是通知栏发起的,然后再去进行操作
private static final int NOTIFY_CONTENT = 0;
private static final int NOTIFY_PRE = 1;
private static final int NOTIFY_NEXT = 2;
private static final String NOTIFY_TYPE = "notifyType";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int notifyType=intent.getIntExtra(NOTIFY_TYPE,-1);
if (notifyType==-1){
//不是通知栏发起的
LogUtil.e(TAG,"AudioPlayService:onStartCommand");
audioItems = (List<AudioItem>) intent.getSerializableExtra("audioItems");
position = intent.getIntExtra("position", -1);
binder.play();
}else{
//从通知栏点击发起
switch (notifyType){
case NOTIFY_NEXT:
binder.next();
break;
case NOTIFY_PRE:
binder.pre();
break;
}
}
return super.onStartCommand(intent, flags, startId);
}
/** 下一首*/
private PendingIntent getNextPendingIntent() {
Intent intent=new Intent(this,AudioPlayService.class);
intent.putExtra(NOTIFY_TYPE, NOTIFY_NEXT);
return PendingIntent.getService(this, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
/** 上一首*/
private PendingIntent getPrePendingIntent() {
Intent intent=new Intent(this,AudioPlayService.class);
intent.putExtra(NOTIFY_TYPE, NOTIFY_PRE);
//注意有多个 PendingIntent 的时候第二个参数不能一样
return PendingIntent.getService(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
6.点击通知栏的空白部分的处理。当点击空白区域的时候需要去开启播放音乐的界面,但是发现界面没有数据,所以我们需要将界面刷新一下,而且注意要将 AudioPlayerActivity 在清单文件中设为 SingleTop 模式,否则点击多次通知会弹出多个页面
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int notifyType=intent.getIntExtra(NOTIFY_TYPE,-1);
//从通知栏点击返回的不是-1
switch (notifyType){
case NOTIFY_NEXT:
binder.next();
break;
case NOTIFY_PRE:
binder.pre();
case NOTIFY_CONTENT:
notifyUpdateUI();
break;
default:
LogUtil.e(TAG,"AudioPlayService:onStartCommand");
audioItems = (List<AudioItem>)
intent.getSerializableExtra("audioItems");
position = intent.getIntExtra("position", -1);
binder.play();
break;
}
return super.onStartCommand(intent, flags, startId);
}
private class OnAudioPreparedListener implements MediaPlayer.OnPreparedListener {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
//准备完成,开始播放
mediaPlayer.start();
//更新界面
notifyUpdateUI();
//显示通知
showNotification();
}
}
/** 更新界面*/
private void notifyUpdateUI() {
//服务发广播来更新界面的图片
Intent intent=new Intent("com.itheima.mediaplay.updateplaybtn");
//获取当前正在播放的音乐
AudioItem audioItem=audioItems.get(position);
intent.putExtra("audioItem",audioItem);
sendBroadcast(intent);
}
7.重复播放不再重新开始播放。判断获取的 position 和之前记录的 position 是否相同,如果相同就是同一首歌,只需要重新加载数据,没有开始播放的话直接让他播放就可以了,如果不是同一首歌曲,将当前的 position 记录下来
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int notifyType=intent.getIntExtra(NOTIFY_TYPE,-1);
//从通知栏点击返回的不是-1
switch (notifyType){
case NOTIFY_NEXT:
binder.next();
break;
case NOTIFY_PRE:
binder.pre();
case NOTIFY_CONTENT:
notifyUpdateUI();
break;
default:
LogUtil.e(TAG,"AudioPlayService:onStartCommand");
int positions = intent.getIntExtra("position", -1);
if (positions==this.position){
//重复播放
notifyUpdateUI();
if (!binder.isPlaying()){
binder.start();
}
}else{
audioItems = (List<AudioItem>)
intent.getSerializableExtra("audioItems");
this.position=positions;
binder.play();
}
break;
}
return super.onStartCommand(intent, flags, startId);
}
小结
这一篇主要是实现了自定义通知栏,大家也可以根据自己所要的界面来添加不同的东西,下一篇则是实现歌词显示,希望大家能继续看下去。