建立一個類專門管理Notification,一個方法顯示,一個方法取消!數組
public class Notification_Manager {
Activity context;
public Notification_Manager(Activity context) {
this.context=context;
}
public void showNotification(){
NotificationManager manager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//參數: 上下文環境, 瞬時消息(一閃而過),時間
Notification notification=new Notification(R.drawable.ic_launcher, "固定資產", System.currentTimeMillis());
/*
//NOtification的高級用法: 播放音頻,震動(須要寫震動權限),LED燈,及默認的設置
Uri soundUri=Uri.fromFile(new File("地址 "));
//播放指定的音頻,即發出提示聲
notification.sound=soundUri;
//設置一個靜止和震動的數組,索引0:靜止時間,索引1:震動時間 ,依次類推
long[] vibrates=new long[]{0,1000,1000,1000};
notification.vibrate=vibrates;
//以寫綠色LED燈爲例:
notification.ledARGB=Color.GREEN; //燈的顏色
notification.ledOffMS=1000; //燈關閉的時間
notification.ledOnMS=1000; //燈亮起的時間
notification.flags=Notification.FLAG_SHOW_LIGHTS;//指示flags的行爲
//如果嫌麻煩,那就用默認的設置:
notification.defaults=Notification.DEFAULT_ALL;
*/
Intent intent=new Intent( context,context.getClass() );
PendingIntent contentIntent= PendingIntent.getActivity(context, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(context,"固定資產", "正在運用", contentIntent);
//參數: id ,notification對象
manager.notify(0, notification);
}
public void cancelNotification(){
NotificationManager manager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//取消id爲0的消息提示框
manager.cancel(0);
}
this
}對象