0

抱歉,如果之前回答过,但我到处找,但没有得到正确的解决方案

我正在使用 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 中,所以我不确定是否仅在每次启动应用程序时才创建警报,并且当我隐藏应用程序时发生了一些奇怪的事情......再次洞察力将不胜感激!

4

2 回答 2

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);
于 2013-07-18T12:57:35.750 回答
0
manager.setRepeating(AlarmManager.RTC_WAKEUP, initTime, 24*60*60*1000, pendingIntent);

警报管理器将立即触发,如果initTime < System.currentTimeMillis()

来自文档

如果时间发生在过去,将立即触发警报,警报计数取决于触发时间相对于重复间隔过去多长时间。

根据您提供的代码,将返回与Today 9.00gcal.getTimeInMillis()对应的毫秒。因此,如果您在此时间之后尝试运行代码,initTime将小于触发 AlarmManager 立即运行的当前系统时间。

例如,要解决此问题,您可以在日历中添加一天,然后再通过它gcal.getTimeInMillis(),如果它已经过去,那么它将指向明天 9.00让它在明天早上运行

更新1

尝试过这样的代码,它对我来说按预期工作 - 每 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 本身中注册一个意图。

于 2013-07-18T13:07:24.280 回答