27

我正在尝试制定时间表。

它应该在每天下午 1 点或 2 点运行...

目前我只能让它每 10 秒或 10 分钟运行一次......

Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.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.SECOND, 10);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

Toast.makeText(AndroidAlarmService.this, "Start Alarm", Toast.LENGTH_LONG).show();

谢谢

4

2 回答 2

101

此代码将在每天下午 1 点或下午 2 点运行 Intent

Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.HOUR_OF_DAY, 13); // For 1 PM or 2 PM
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
PendingIntent pi = PendingIntent.getService(context, 0,
            new Intent(context, MyClass.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                                AlarmManager.INTERVAL_DAY, pi);
于 2011-07-27T09:11:02.593 回答
-10

这每天都会报警。

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,  AlarmManager.INTERVAL_DAY , pendingIntent);
于 2010-12-30T13:06:05.437 回答