我是个android的新手,一直在研究闹钟。我想要在生日那天提醒。我已经使用了Alarm Manager,但看到有些人说它会在重启后清除,这让我很困惑。由于没有安卓手机,所以我只能使用模拟器。
以下是我的代码:
public void schedAlarm() {
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, AlarmService.class);
pendingIntent = PendingIntent.getBroadcast(this, contact.id, intent, PendingIntent.FLAG_ONE_SHOT);
am.setRepeating(AlarmManager.RTC, timetoAlarm, nextalarm, pendingIntent);
}
我用这个BroadcastRecever代替了AlarmSerivce,代码如下:
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "It Birthday!";
CharSequence message =" Greet your friend.";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
Notification notif = new Notification(R.drawable.ic_launcher, "Birthday", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(1, notif);
}
这足够了吗?