为什么我的 AlarmManager setRepeating 不重复?

发布于 2024-11-30 19:03:45 字数 1205 浏览 1 评论 0原文

我创建了一个 AlarmManager,它应该重复运行并启动服务。我的问题是它只运行一次。我不知道出了什么问题。现在,我已将 AlarmManager 的创建附加到按钮上。

这是 MenuActivity 类的一部分:

private void startUpdateService() {
    Log.i(TAG, "Registering AlarmManager");

    Intent service = new Intent(MenuActivity.this, UpdateService.class);
    pendingIntent = PendingIntent.getService(MenuActivity.this, 0, service, 0);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000, 10*1000, pendingIntent);

}

@Override
public void onClick(View view) {
    switch(view.getId()) {
    case R.id.button_calendar:
        Intent calendarIntent = new Intent(this, CalendarActivity.class);
        if(Util.isOnline(this)){
            startActivity(calendarIntent);
        }else {
            Toast.makeText(MenuActivity.this, R.string.toast_is_online, Toast.LENGTH_LONG).show();
        }
        break;
    case R.id.button_settings:
        startUpdateService();           
        Toast.makeText(MenuActivity.this, "Startet alarm", Toast.LENGTH_LONG).show();           
        break;
    }

}

初始警报工作正常,但不会重复。 有人知道我做错了什么吗?

I've created an AlarmManager that should run repeatedly and start a service. My problem is it only runs once. I can't figure out what is wrong. For now I've attached creation of the AlarmManager to a button.

This is part of the MenuActivity class:

private void startUpdateService() {
    Log.i(TAG, "Registering AlarmManager");

    Intent service = new Intent(MenuActivity.this, UpdateService.class);
    pendingIntent = PendingIntent.getService(MenuActivity.this, 0, service, 0);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000, 10*1000, pendingIntent);

}

@Override
public void onClick(View view) {
    switch(view.getId()) {
    case R.id.button_calendar:
        Intent calendarIntent = new Intent(this, CalendarActivity.class);
        if(Util.isOnline(this)){
            startActivity(calendarIntent);
        }else {
            Toast.makeText(MenuActivity.this, R.string.toast_is_online, Toast.LENGTH_LONG).show();
        }
        break;
    case R.id.button_settings:
        startUpdateService();           
        Toast.makeText(MenuActivity.this, "Startet alarm", Toast.LENGTH_LONG).show();           
        break;
    }

}

Initial alarm is working fine, but it won't repeat.
Anyone that knows what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似狗非友 2024-12-07 19:03:45

我见过直接从警报运行服务的一些问题。我也有过。我所做的是用启动服务的广播接收器替换该服务。你为什么不也尝试一下呢? Advanced Android 对此进行了很好的解释,版本 1 可通过 CC 许可证获得。

I have seen few problems with running service straight from the alarm. I had it too. What i did, was replacing the service by the broadcast reciver which fires up the service. Why won't you try it too? It's explained well in Advanced Android, ver 1 is available on CC licence.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文