Android NotificationManager 和Notification的使用總結

         
使用系統定義的Notification
005 以下是使用示例代碼:
006 //創建一個NotificationManager的引用
007 String ns = Context.NOTIFICATION_SERVICE;
008 NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);
009 // 定義Notification的各種屬性
010 int icon = R.drawable.icon; //通知圖標
011 CharSequence tickerText = "Hello"//狀態欄顯示的通知文本提示
012 long when = System.currentTimeMillis(); //通知產生的時間,會在通知信息裏顯示
013 //用上面的屬性初始化 Nofification
014 Notification notification = new Notification(icon,tickerText,when);
015 /*
 完整實例代碼:
      Notification notification=new Notification();
     notification.icon=R.drawable.ic_launcher;
    notification.tickerText="我是最棒的";
    /**單擊通知後的Intent,此例子單擊後還是在當前頁面*/
   PendingIntent pendingIntent=PendingIntent.getActivity(PlayMusicRecevicer.this, 0, new                      Intent(PlayMusicRecevicer.this,PlayMusicRecevicer.class), 0);
   notification.setLatestEventInfo(PlayMusicRecevicer.this, "你..是最棒的", "堅持就是勝利", pendingIntent);
  notificationManager.notify(0,notification);

   
添加聲音
notification.defaults |=Notification.DEFAULT_SOUND;
或者使用以下幾種方式
notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
如果想要讓聲音持續重複直到用戶對通知做出反應,則可以在notification的flags字段增加"FLAG_INSISTENT"
如果notification的defaults字段包括了"DEFAULT_SOUND"屬性,則這個屬性將覆蓋sound字段中定義的聲音

Notification notification2=new Notification();
//String ringNameString=RingtoneManager.getActualDefaultRingtoneUri(PlayMusicRecevicer.this, RingtoneManager.TYPE_RINGTONE).toString();
//notification2.sound=Uri.parse(ringNameString);
notification2.vibrate = new long[] {0, 100, 200, 300};// 設置通知震動模式
notificationManager.notify(0, notification2);


添加振動
notification.defaults |= Notification.DEFAULT_VIBRATE;
或者可以定義自己的振動模式:
long[] vibrate = {0,100,200,300}; //0毫秒後開始振動,振動100毫秒後停止,再過200毫秒後再次振動300毫秒
notification.vibrate = vibrate;
long數組可以定義成想要的任何長度
如果notification的defaults字段包括了"DEFAULT_VIBRATE",則這個屬性將覆蓋vibrate字段中定義的振動

Notification notification2=new Notification();
String ringNameString=RingtoneManager.getActualDefaultRingtoneUri(PlayMusicRecevicer.this, RingtoneManager.TYPE_RINGTONE).toString();
notification2.sound=Uri.parse(ringNameString);
notificationManager.notify(0, notification2);


添加LED燈提醒
notification.defaults |= Notification.DEFAULT_LIGHTS;
或者可以自己的LED提醒模式:
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300; //亮的時間
notification.ledOffMS = 1000; //滅的時間
notification.flags |= Notification.FLAG_SHOW_LIGHTS;


更多的特徵屬性
notification.flags |= FLAG_AUTO_CANCEL; //在通知欄上點擊此通知後自動清除此通知
notification.flags |= FLAG_INSISTENT; //重複發出聲音,直到用戶響應此通知
notification.flags |= FLAG_ONGOING_EVENT; //將此通知放到通知欄的"Ongoing"即"正在運行"組中
notification.flags |= FLAG_NO_CLEAR; //表明在點擊了通知欄中的"清除通知"後,此通知不清除,
經常與FLAG_ONGOING_EVENT一起使用
notification.number = 1; //number字段表示此通知代表的當前事件數量,它將覆蓋在狀態欄圖標的頂部
如果要使用此字段,必須從1開始
notification.iconLevel = ; //

  二
使用自定義的 Notification
要創建一個自定義的Notification,可以使用RemoteViews。要定義自己的擴展消息,首先 要初始化一個RemoteViews對象,然後將它傳遞給Notification的contentView字段,再把PendingIntent傳遞給 contentIntent字段。以下示例代碼是完整步驟:
以下是全部示例代碼
//創建一個 NotificationManager的引用
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);
// 定義Notification的各種屬性
int icon = R.drawable.icon; //通知圖標
CharSequence tickerText = "Hello";//狀態欄顯示的通知文本提示
long when = System.currentTimeMillis(); //通知產生的時間,會在通知信息裏顯示
//用上面的屬性初始化 Nofification
Notification notification = new Notification(icon,tickerText,when);
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.view);
contentView.setImageViewResource(R.id.image, R.drawable.iconempty);
contentView.setTextViewText(R.id.text,"Hello,this is JC");
notification.contentView = contentView;
Intent notificationIntent = new Intent(this,Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
notification.contentIntent = contentIntent;
//把Notification傳遞給NotificationManager
mNotificationManager.notify(0,notification);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章