在這里把對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);
}
}