轉載請註明出處:http://blog.csdn.net/linglongxin24/article/details/53166551
本文出自【DylanAndroid的博客】
全新的Android通知欄,已拋棄setLatestEventInfo,兼容高版本
這算是一個入門級的Android通知欄notification的文章,因爲在項目中要用到,又發現以前的低版本的用setLatestEventInfo已過時,還報錯,完全不兼容。所以,在這裏介紹下基本用法,代碼比較簡單,高手請略過。上圖:
1.主要參數介紹
- 1.notification的title
- 2.發送notification的application的icon或者發送者的image。
- 3.notification的message
- 4.notification的數目顯示
- 5.當main icon用來顯示sender’s image時 Secondary icon用來顯示發送application的icon。
- 6.時間戳,默認爲系統發出通知的時間。
2.顯示一個普通的通知欄
/**
* 顯示一個普通的通知
*
* @param context 上下文
*/
public static void showNotification(Context context) {
Notification notification = new NotificationCompat.Builder(context)
/**設置通知左邊的大圖標**/
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
/**設置通知右邊的小圖標**/
.setSmallIcon(R.mipmap.ic_launcher)
/**通知首次出現在通知欄,帶上升動畫效果的**/
.setTicker("通知來了")
/**設置通知的標題**/
.setContentTitle("這是一個通知的標題")
/**設置通知的內容**/
.setContentText("這是一個通知的內容這是一個通知的內容")
/**通知產生的時間,會在通知信息裏顯示**/
.setWhen(System.currentTimeMillis())
/**設置該通知優先級**/
.setPriority(Notification.PRIORITY_DEFAULT)
/**設置這個標誌當用戶單擊面板就可以讓通知將自動取消**/
.setAutoCancel(true)
/**設置他爲一個正在進行的通知。他們通常是用來表示一個後臺任務,用戶積極參與(如播放音樂)或以某種方式正在等待,因此佔用設備(如一個文件下載,同步操作,主動網絡連接)**/
.setOngoing(false)
/**向通知添加聲音、閃燈和振動效果的最簡單、最一致的方式是使用當前的用戶默認設置,使用defaults屬性,可以組合:**/
.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
.setContentIntent(PendingIntent.getActivity(context, 1, new Intent(context, MainActivity.class), PendingIntent.FLAG_CANCEL_CURRENT))
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
/**發起通知**/
notificationManager.notify(0, notification);
}
3.顯示一個下載帶進度條的通知
/**
* 顯示一個下載帶進度條的通知
*
* @param context 上下文
*/
public static void showNotificationProgress(Context context) {
//進度條通知
final NotificationCompat.Builder builderProgress = new NotificationCompat.Builder(context);
builderProgress.setContentTitle("下載中");
builderProgress.setSmallIcon(R.mipmap.ic_launcher);
builderProgress.setTicker("進度條通知");
builderProgress.setProgress(100, 0, false);
final Notification notification = builderProgress.build();
final NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
//發送一個通知
notificationManager.notify(2, notification);
/**創建一個計時器,模擬下載進度**/
Timer timer = new Timer();
timer.schedule(new TimerTask() {
int progress = 0;
@Override
public void run() {
Log.i("progress", progress + "");
while (progress <= 100) {
progress++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//更新進度條
builderProgress.setProgress(100, progress, false);
//再次通知
notificationManager.notify(2, builderProgress.build());
}
//計時器退出
this.cancel();
//進度條退出
notificationManager.cancel(2);
return;//結束方法
}
}, 0);
}
4.顯示一個懸掛式的通知
/**
* 懸掛式,部分系統廠商不支持
*
* @param context
*/
public static void showFullScreen(Context context) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/linglongxin24"));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mIntent, 0);
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));
builder.setAutoCancel(true);
builder.setContentTitle("懸掛式通知");
//設置點擊跳轉
Intent hangIntent = new Intent();
hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
hangIntent.setClass(context, MainActivity.class);
//如果描述的PendingIntent已經存在,則在產生新的Intent之前會先取消掉當前的
PendingIntent hangPendingIntent = PendingIntent.getActivity(context, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setFullScreenIntent(hangPendingIntent, true);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(3, builder.build());
}
5.顯示一個摺疊式的通知
/**
* 摺疊式
*
* @param context
*/
public static void shwoNotify(Context context) {
//先設定RemoteViews
RemoteViews view_custom = new RemoteViews(context.getPackageName(), R.layout.view_custom);
//設置對應IMAGEVIEW的ID的資源圖片
view_custom.setImageViewResource(R.id.custom_icon, R.mipmap.icon);
view_custom.setTextViewText(R.id.tv_custom_title, "今日頭條");
view_custom.setTextColor(R.id.tv_custom_title, Color.BLACK);
view_custom.setTextViewText(R.id.tv_custom_content, "金州勇士官方宣佈球隊已經解僱了主帥馬克-傑克遜,隨後宣佈了最後的結果。");
view_custom.setTextColor(R.id.tv_custom_content, Color.BLACK);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContent(view_custom)
.setContentIntent(PendingIntent.getActivity(context, 4, new Intent(context, MainActivity.class), PendingIntent.FLAG_CANCEL_CURRENT))
.setWhen(System.currentTimeMillis())// 通知產生的時間,會在通知信息裏顯示
.setTicker("有新資訊")
.setPriority(Notification.PRIORITY_HIGH)// 設置該通知優先級
.setOngoing(false)//不是正在進行的 true爲正在進行 效果和.flag一樣
.setSmallIcon(R.mipmap.icon);
Notification notify = mBuilder.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(4, notify);
}