很抱歉,如果以前回答,但我到处寻找,但没有得到正确的解决方案
我正在使用AlarmManager在每天早上9点自动发出通知,但是当我尝试在模拟器上运行它时会立即执行,并且每半小时(准确地说是31-32分钟)而不是每天早上9点执行一次
任何想法为什么?帮助赞赏。
代码如下:
public class Home extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bsheet);
notificationAlert(savedInstanceState);
}
private void notificationAlert(Bundle savedInstanceState) {
AlarmManager manager;
PendingIntent pendingIntent;
Intent intent=new Intent(Home.this, Notify.class);
manager=(AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent=PendingIntent.getService(Home.this,
0, intent, 0);
GregorianCalendar gcal = new GregorianCalendar();
gcal.set(Calendar.HOUR_OF_DAY, 9);
gcal.set(Calendar.MINUTE, 0);
gcal.set(Calendar.SECOND, 0);
gcal.set(Calendar.MILLISECOND, 0);
long initTime = gcal.getTimeInMillis();
manager.setRepeating(AlarmManager.RTC_WAKEUP, initTime,
24*60*60*1000, pendingIntent);
}
}
欢呼声,
编辑:我的意图是,一旦安装了应用程序,它会在上午9点触发此警报。我已经将警报放在onCreate中,所以我不确定每次启动应用程序时是否只创建警报,当我隐藏应用程序时会发生一些奇怪的事情...再一次见识会很感激!
答案 0 :(得分:0)
试试这个:
Calendar currentTime = Calendar.getInstance();
Calendar alarmTime = Calendar.getInstance();
currentTime.set(Calendar.HOUR_OF_DAY, 9;
currentTime.set(Calendar.MINUTE, 0);
currentTime.set(Calendar.SECOND, 0);
currentTime.set(Calendar.MILLISECOND, 0);
if (alarmTime.compareTo(currentTime) <= 0) {
// check for time
alarmTime.add(Calendar.DATE, 1);
}
Intent intent = new Intent(getBaseContext(), Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getBaseContext(), 1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
答案 1 :(得分:0)
manager.setRepeating(AlarmManager.RTC_WAKEUP, initTime, 24*60*60*1000, pendingIntent);
如果initTime < System.currentTimeMillis()
如果过去发生时间,将立即触发警报,警报计数取决于过去触发时间相对于重复间隔的距离。
根据您提供的代码,gcal.getTimeInMillis()
将返回与今天9.00 对应的毫秒代码。因此,如果您在此之后尝试运行代码, initTime 将小于当前系统时间,从而触发AlarmManager的即时运行。
要解决此问题,例如,您可以在传递gcal.getTimeInMillis()
之前向日历添加一天(如果已经过去),那么它将指向明天9.00 以让它运行明天早上
<强> UPDATE1 强>
尝试这样的代码并按预期工作 - 每10秒启动一次服务:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initManager();
}
private void initManager() {
AlarmManager manager;
PendingIntent pendingIntent;
Intent intent = new Intent(this, NotifyService.class);
manager=(AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent=PendingIntent.getService(this, 0, intent, 0);
long initTime = System.currentTimeMillis() + 5000;
manager.setRepeating(AlarmManager.RTC_WAKEUP, initTime,
10*1000, pendingIntent);
}
但是您可以使用其他选项:AlarmManager.set()
方法
你可以像这样点燃它
long runTime = /* your calendar time + 24 hours in millis*/
manager.set(AlarmManager.RTC_WAKEUP, runTime, pendingIntent);
因此它会在runTime
启动您的服务。然后在服务中调用相同的方法重新计算runTime
一天,所以看起来像这样:
public MainActivity extends Activity{
protected onCreate(Bundle bundle){
....
initManager();
}
private initManager(){
...
long runTime = /* time of your first alarm/notification/whatever you develope */
Intent intent = new Intent(this, NotifyService.class);
pendingIntent=PendingIntent.getService(this, 0, intent, 0);
manager.set(AlarmManager.RTC_WAKEUP, runTime, pendingIntent);
}
}
服务:
public NotifyService extends Service{
...
public int onStartCommand(...){
...
/* do stuff */
Intent intent = new Intent(getApplicationContext(), NotifyService.class);
pendingIntent=PendingIntent.getService(this, 0, intent, 0);
long runTime = /* recalculate it according to the next time of firing this service*/
manager.set(AlarmManager.RTC_WAKEUP, runTime, pendingIntent);
}
}
因此,每次服务触发时,您的服务都会在AlarmManager中注册一个意图。