package com.example.test_ipod_demo;
import android.R.anim;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;
/**
* @author luohao 构造单例模式
*/
public class IpodNotificationBuilder {
private Context context = null;
private static IpodNotificationBuilder notificationBuilder = null;
private NotificationManager notificationManager = null;
private NotificationCompat.Builder notification = null;
/**
* 自定义视图
*/
private RemoteViews remoteViews = null;
/**
* 事件广播
*/
private Intent broadIntent = null;
/**
* 歌手名
*/
private String singer_name = "未知歌手";
/**
* 歌曲名
*/
private String song_name = "未知歌曲";
private IpodNotificationBuilder(Context context) {
this.context = context;
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notification = new NotificationCompat.Builder(context);
//设置显示视图
remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_paly_notify);
setSingerName(singer_name);
setSongrName(song_name);
setSongPicture(android.R.drawable.ic_dialog_info);
broadIntent = new Intent(NotifyReciever.NOTIFY_ACTION);
// 上一首
setOnClickData(R.id.imageButton_previous, NotifyReciever.VALUE_PREVIOUS, 0);
// 播放/暂停
setOnClickData(R.id.imageButton_play, NotifyReciever.VALUE_PALY_PAUSE, 1);
setOnClickData(R.id.ImageButton_next, NotifyReciever.VALUE_NEXT, 2);
setOnClickData(R.id.layout_info, NotifyReciever.VALUE_OPEN_ACTIVITY, 3);
setSmallIcon(android.R.drawable.ic_dialog_email);
// notification.setSmallIcon(android.R.drawable.ic_dialog_email);//
// 必须设置此图标,否则notify不显示:此图标为,通知栏显示的小图标
// notificationManager.notify(1, notification.build());
}
public static IpodNotificationBuilder getInstance(Context context) {
if (notificationBuilder == null) {
notificationBuilder = new IpodNotificationBuilder(context);
}
return notificationBuilder;
}
/**
* 设置自定义视图中的 控件click响应 以及 数据
*
* @param viewId 控件Id
* @param value 事件代码
* @param requestCode 区分pendingIntent 设置为一样的,后面的会覆盖前面的事件与数据
*/
private void setOnClickData(int viewId, int value, int requestCode) {
broadIntent.putExtra(NotifyReciever.NOTIFY_KEY, value);
PendingIntent pendingIntent_pre = PendingIntent.getBroadcast(context, requestCode, broadIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(viewId, pendingIntent_pre);
}
/**
* 设置通知栏显示小图标
*
* @param icon
* @return
*/
public IpodNotificationBuilder setSmallIcon(int icon) {
notification.setSmallIcon(icon);
return this;
}
/**
* 设置歌曲专辑图片
*
* @param viewId
* @param srcId
* @return
*/
public IpodNotificationBuilder setSongPicture(int srcId) {
remoteViews.setImageViewResource(R.id.imageView_song, srcId);
return this;
}
/**
* 设置歌曲专辑图片
*
* @param viewId
* @param bitmap
* @return
*/
public IpodNotificationBuilder setSongPicture(Bitmap bitmap) {
remoteViews.setImageViewBitmap(R.id.imageView_song, bitmap);
return this;
}
/**
* 设置歌手名
*
* @param singerName
* @return
*/
public IpodNotificationBuilder setSingerName(String singerName) {
remoteViews.setTextViewText(R.id.textView_singer, singerName);
return this;
}
/**
* 设置 歌曲名
*
* @param songName
* @return
*/
public IpodNotificationBuilder setSongrName(String songName) {
remoteViews.setTextViewText(R.id.textView_song, songName);
return this;
}
/**
* 切换 播放/暂停 图标
*
* @param isPlay
* 是否正在播放
* @return
*/
public IpodNotificationBuilder switchImagePaly_Pause(boolean isPlay) {
if (isPlay) {
remoteViews.setImageViewResource(R.id.imageButton_play, android.R.drawable.ic_media_play);
} else {
remoteViews.setImageViewResource(R.id.imageButton_play, android.R.drawable.ic_media_pause);
}
return this;
}
/**
* 创建并显示
*
* @param isClear
* 是否可被清除
*/
public void buildShow(boolean isClear) {
notification.setContent(remoteViews);
Notification notification2 = notification.build();
if (isClear) {// 可被清除
notification2.flags = Notification.FLAG_AUTO_CANCEL;
} else {
notification2.flags = Notification.FLAG_NO_CLEAR;
}
notificationManager.notify(320, notification2);
// notification.setAutoCancel(true);
// notificationManager.notify(320, notification.build());
}
}
注意点:
1.小图标要设置,否则不显示
notification.setSmallIcon(android.R.drawable.ic_dialog_email);
// 必须设置此图标,否则notify不显示:此图标为,通知栏显示的小图标
2.事件以广播的形式发送,通知后台,由于不同的操作对应不同的事件,所以requestCode 要设置为不一样的,以区分不同的PendingIntent,否则后面的事件与数据会 覆盖前面的。
PendingIntent pendingIntent_pre = PendingIntent.getBroadcast(context, requestCode, broadIntent, PendingIntent.FLAG_UPDATE_CURRENT);
3.设置 通知栏是否可被清除
Notification notification2 = notification.build();
if (isClear) {// 可被清除
notification2.flags = Notification.FLAG_AUTO_CANCEL;
} else {
notification2.flags = Notification.FLAG_NO_CLEAR;
}
notificationManager.notify(320, notification2);
广播处理类:
package com.example.test_ipod_demo;
import android.R.string;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
/**
* 通知栏 操作广播通知处理
*
* @author luohao
*
*/
public class NotifyReciever extends BroadcastReceiver {
public static final String NOTIFY_ACTION = "action.ipod.notify";
public static final String NOTIFY_KEY = "CMD";
public static final int VALUE_OPEN_ACTIVITY = 0;
public static final int VALUE_PALY_PAUSE = 1;
public static final int VALUE_NEXT = 2;
public static final int VALUE_PREVIOUS = 3;
String tag = "MReciever";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(tag, intent.getAction());
Log.i(tag, String.valueOf(intent.getIntExtra(NOTIFY_KEY, 10)));
switch (intent.getExtras().getInt(NOTIFY_KEY, VALUE_OPEN_ACTIVITY)) {
case VALUE_OPEN_ACTIVITY:
Log.i(tag, "打开正在播放");
Toast.makeText(context, "跳转到正在播放", 1000).show();
break;
case VALUE_PALY_PAUSE:
Log.i(tag, "播放/暂停");
Toast.makeText(context, "播放/暂停", 1000).show();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
IpodNotificationBuilder.getInstance(context).switchImagePaly_Pause(false).buildShow(false);
break;
case VALUE_NEXT:
Log.i(tag, "下一首");
Toast.makeText(context, "下一首", 1000).show();
break;
case VALUE_PREVIOUS:
Log.i(tag, "上一首");
Toast.makeText(context, "上一首", 1000).show();
break;
default:
break;
}
}
}