为什么不使用常规通知?
您可以对其进行配置,使其可以使手机振动、发出声音,甚至更改 LED 颜色(如果支持)。
这是我一直用来提供通知的简单方法:
注意:为了使振动起作用,您必须在清单中添加:
<uses-permission android:name="android.permission.VIBRATE" >
方法:
public static void sendNotification(Context caller, Class<?> activityToLaunch, String title, String msg, int numberOfEvents,boolean sound, boolean flashLed, boolean vibrate,int iconID) {
NotificationManager notifier = (NotificationManager) caller.getSystemService(Context.NOTIFICATION_SERVICE);
final Notification notify = new Notification(iconID, "", System.currentTimeMillis());
notify.icon = iconID;
notify.tickerText = title;
notify.when = System.currentTimeMillis();
notify.number = numberOfEvents;
notify.flags |= Notification.FLAG_AUTO_CANCEL;
if (sound) notify.defaults |= Notification.DEFAULT_SOUND;
if (flashLed) {
// add lights
notify.flags |= Notification.FLAG_SHOW_LIGHTS;
notify.ledARGB = Color.CYAN;
notify.ledOnMS = 500;
notify.ledOffMS = 500;
}
if (vibrate) {
notify.vibrate = new long[] {100, 200, 300};
}
Intent toLaunch = new Intent(caller, activityToLaunch);
toLaunch.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
toLaunch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intentBack = PendingIntent.getActivity(caller, number, toLaunch, 0);
notify.setLatestEventInfo(caller, title, msg, intentBack);
notifier.notify(number, notify);
number = number+1;
}