您好我正在处理一个应用程序,我将从用户收到提醒详细信息作为提醒日期和提醒名称并将其存储在数据库中。现在我想知道如何通过后台服务开始提醒?或者有其他方式,如果是,请告诉我。我该怎么办?请帮我。建议一些东西或教程将是个好主意。 提前谢谢!
答案 0 :(得分:0)
为此你需要两件事
在您要设置提醒的活动类中执行此操作
Intent myIntent = new Intent(this , NotifyService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
//get time from database and initialise the variables.
int minute;
int hour;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.HOUR, hour);
calendar.set(Calendar.AM_PM, Calendar.AM); //set accordingly
calendar.add(Calendar.DAY_OF_YEAR, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
这将在您设定的时间每天触发警报。
现在,您应该将服务创建为NotifyService并将以下代码放在onCreate()中:
@Override
public void onCreate() {
NotificationManager mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.notification_icon, "Notify Alarm strart", System.currentTimeMillis());
Intent myIntent = new Intent(this , MyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "Notify label", "Notify text", contentIntent);
mNM.notify(NOTIFICATION, notification);