我有一些代码想要在每天凌晨3:00执行。我已经读过Service Class Documentation了,似乎我可以使用AlarmManager来触发一个意图(我想是活动还是服务?),然后在这个意图中创建一条消息并在安卓通知区域发布。
Calendar threeAM = Calendar.getInstance();
threeAM.set(Calendar.HOUR_OF_DAY,2);
threeAM.set(Calendar.MINUTE,0);
threeAM.set(Calendar.SECOND,0);
threeAM.set(Calendar.MILLISECOND,0);
AlarmManager alarmManager =
(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, myNotifier.class);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, threeAM.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, PendingIntent.getService(context, 1, i , 0));
Log.i("Service TEST", "Alarm set?" );它在代码中运行,没有任何问题,但没有设置警报的迹象,活动也不会启动。我正在使用一个我知道有效的活动。我试着把它包装在try/catch中,在logcat中什么都没有...
发布于 2011-02-20 05:06:31
我认为你应该注册你的活动来接收启动广播接收器,这样你的应用程序就会在操作系统启动完成的那一刻启动。这里是链接http://www.androidenea.com/2009/09/starting-android-service-after-boot.html
它不会在通知方面有所帮助,但它将解决您的活动无法启动的问题……
发布于 2011-02-20 05:07:48
您的代码只有在凌晨3点前执行才能正常工作否则,您将在过去设置警报。
https://stackoverflow.com/questions/5053505
复制相似问题