我正在开发一个Android应用程序,但问题是我必须通过通知栏上的消息提醒用户,这将在十(10)天后发生,就像在糖果粉碎游戏中它会在几小时后提供完整生命警报。我是android的初学者,所以我对这个场景的实现感到困惑。请用代码指导我的实现。
答案 0 :(得分:2)
您可以使用AlarmManager和BroadcastReceiver
执行此操作Intent myIntent = new Intent(this, TimeAlarm.class);
pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.DAY_OF_MONTH, 10);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
public class TimeAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent paramIntent) {
// Request the notification manager
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Create a new intent which will be fired if you click on the notification
Intent intent = new Intent("android.intent.action.VIEW");
// Attach the intent to a pending intent
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Create the notification
Notification notification = new Notification(R.drawable.icon, "Something new!"), System.currentTimeMillis());
notification.setLatestEventInfo(context, "Something new!", "Description",pendingIntent);
// Fire the notification
notificationManager.notify(1, notification);
}
}