在这里把对android notification经常会用到的用法大概说明一下,由于时间问题,关于不同系统中的兼容性问题,还不及做太多的测试。什么时候有时间再补上。
实现了酷狗的Notification中播放器的界面
还是那句话:有时间再把关于Notification的用法和要注意的东西补完整。
package com.lee.notification;
import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build.VERSION;
import android.util.Log;
import android.widget.RemoteViews;
public class NotificationMusicBox {
private String TAG = "NotificationMusicBox_Task";
private int Notification_Id = 999;
private Notification notification;
private NotificationManager notificationManager;
private Activity mActivity;
private RemoteViews bigContentView,contentView;
private int sysVersion = Integer.parseInt(VERSION.SDK);
public NotificationMusicBox(Activity activity){
this.mActivity = activity;
init();
}
/**
* 初始化操作
* notification.contentView 和 notification.bigContentView的不同之处:
* notification.contentView是单行的通知
* notification.bigContentView可以显示更多内容的通知
*
* 在4.1系统的时候
* 在当前的notification不是在通知栏顶部的时候是显示notification.contentView布局
* 在当前的notification是在通知栏的顶部的时候显示的是notification.bigContentView布局
*
* 在4.0系统的时候
* 关于notification的bigContentView是在Jelly Bean 4.1之中加入的,在4.1之前的话bigContentView属性是无效的。
*
* 只有在在sdk3.0以上的系统中,通知栏中的按钮点击事件才能响应,以下的做不到。
* 要加入按钮点击事件监听的话,必须要做手机的系统判断。再使用RemoteViews的setOnClickPendingIntent的方法
*
* 在3.0之前推荐的通知栏写法是
* notification = new Notification(R.drawable.bg_albumdetail_default, "start listener Music", System.currentTimeMillis());
* notification.setLatestEventInfo(this.mActivity, "this is test", "ok!", contentPendingIntent);
* 此时notification的logo是实例化Notification中的 "R.drawable.bg_albumdetail_default"
* 实际内容是通过setLatestEventInfo(this.mActivity, "this is test", "ok!", contentPendingIntent);
*/
private void init(){
Log.d(TAG, String.valueOf(sysVersion));
//设置基本的notification
initNotification();
//时间匆忙,还不及去测试不同系统的兼容性
if(sysVersion>=15){
setContetView();
setBigContentView();
}
}
/**
* 初始化Notification
* 此处做Notification的基本设置
*/
private void initNotification(){
//整个notification的点击事件
Intent contentIntent = new Intent(this.mActivity, NotificationShow.class);
contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
contentIntent.putExtra("Key", "尼玛");
PendingIntent contentPendingIntent = PendingIntent.getActivity(this.mActivity,R.string.app_name,contentIntent,PendingIntent.FLAG_UPDATE_CURRENT);
//获取notification管理的实例
notificationManager = (NotificationManager) this.mActivity.getSystemService(Context.NOTIFICATION_SERVICE);
//设置notification
notification = new Notification(R.drawable.bg_albumdetail_default, "start listener Music", System.currentTimeMillis());
notification.setLatestEventInfo(this.mActivity, "this is test", "ok!", contentPendingIntent);
notification.flags = Notification.FLAG_NO_CLEAR;
notification.contentIntent = contentPendingIntent;
}
/**
* 设置contentView
* 注:只有在在sdk3.0以上的系统中,通知栏中的按钮点击事件才能响应,以下的做不到。
*/
private void setContetView(){
//布局文件(小)
contentView = new RemoteViews(this.mActivity.getPackageName(),R.layout.notification_view);
contentView.setTextViewText(R.id.tv_music, "Hello World!");
notification.contentView = contentView;
}
/**
* 设置BigContentView
* 在Jelly Bean 中加入
* 在此可以加入按钮的事件监听
*/
private void setBigContentView(){
//布局文件(大)
bigContentView = new RemoteViews(this.mActivity.getPackageName(),R.layout.notification_playmusic);
bigContentView.setTextViewText(R.id.tv_music, "Hello World!");
//上一页
Intent lastIntent = new Intent("com.lee.notificationReceiver");
PendingIntent lastPendingIntent = PendingIntent.getBroadcast(this.mActivity,0,lastIntent,PendingIntent.FLAG_UPDATE_CURRENT);
bigContentView.setOnClickPendingIntent(R.id.ib_last, lastPendingIntent);
//播放或暂停
Intent playOrPauseIntent = new Intent(this.mActivity,NotificationShow.class);
PendingIntent playOrPausePendingIntent = PendingIntent.getBroadcast(this.mActivity,1,playOrPauseIntent,PendingIntent.FLAG_UPDATE_CURRENT);
bigContentView.setOnClickPendingIntent(R.id.ib_playOrPause, playOrPausePendingIntent);
//下一页
Intent nextIntent = new Intent(this.mActivity,NotificationShow.class);
PendingIntent nextPendingIntent = PendingIntent.getActivity(this.mActivity,1,nextIntent,PendingIntent.FLAG_UPDATE_CURRENT);
bigContentView.setOnClickPendingIntent(R.id.ib_next, nextPendingIntent);
notification.bigContentView = bigContentView;
}
/**
* 显示notificationMusicBox
*/
public void notifyMusicBox(){
notificationManager.notify(Notification_Id, notification);
}
}