- 在狀態顯示您的圖示,另外使用者將狀態列往下划時,會看到較詳細的提醒說明,若使用者按一下該說明,還可以執行您指定的程式。
- 打開設備的 LED 燈 或 讓 LED 燈閃爍
- 撥放聲音
- 讓手機振動
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mm = (NotificationManager) getSystemService(ns);
// 指定顯示在狀態列的圖示
int icon = R.drawable.logo;
CharSequence tickerText = "今天值班";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
// 指定 notification 預設動作, 以 OR 方式給值
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
// 全都使用預設值
notification.defaults |= Notification.DEFAULT_ALL;
// 撥放自己指定的聲音或音樂
notification.sound = Uri.parse("android.resource://package 名稱/raw/hskay");
CharSequence contentTitle = "值班提醒";
CharSequence contentText = "提醒您,今天值班";
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
int HELLO_ID = 1;
mm.notify(HELLO_ID, notification);
NotificationManager 是 API Level 1 的類別,如果您的 app 最低需求是 Android 3.0(HONEYCOMB),倒可以改用 Notification.Builder。