当设备关闭并再次打开时,闹钟在 Android 中不起作用

发布于 2024-10-31 15:08:42 字数 799 浏览 1 评论 0原文

我已经在 Android 中设置了闹钟来提醒我,当设备打开时它正在工作。但是当我关闭设备并再次打开时,提醒警报不起作用。你们能建议我如何解决这个问题吗?

我的代码看起来像这样,

Intent myIntent = new Intent(getApplicationContext(), serviceclass.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),
    CONST+id, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Calendar calender = Calendar.getInstance();
calender.setTimeInMillis(System.currentTimeMillis());
calender.set(Calendar.HOUR_OF_DAY, hours);
calender.set(Calendar.MINUTE, ireminder.getMin());
calender.set(Calendar.SECOND, 0);
calender.set(Calendar.MILLISECOND, 0);  
calender.set(Calendar.DAY_OF_WEEK, day);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,  

calender.getTimeInMillis(), 7 * AlarmManager.INTERVAL_DAY, pendingIntent);                

I have set the alarm to remind me in android it is working when the device is on. But when i switch off the device and again on that reminder alarm is not working. Can you guys please suggest me how can solve this problem?

My code looks like this,

Intent myIntent = new Intent(getApplicationContext(), serviceclass.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),
    CONST+id, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Calendar calender = Calendar.getInstance();
calender.setTimeInMillis(System.currentTimeMillis());
calender.set(Calendar.HOUR_OF_DAY, hours);
calender.set(Calendar.MINUTE, ireminder.getMin());
calender.set(Calendar.SECOND, 0);
calender.set(Calendar.MILLISECOND, 0);  
calender.set(Calendar.DAY_OF_WEEK, day);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,  

calender.getTimeInMillis(), 7 * AlarmManager.INTERVAL_DAY, pendingIntent);                

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

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

发布评论

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

评论(2

冰雪梦之恋 2024-11-07 15:08:42

重新启动后警报将被清除。

您可以做的是

  1. 将警报信息保留在数据库表
  2. 寄存器中以用于 REBOOT_COMPLETED - 事件
  3. 重新启动时,启动一个后台线程来重新注册警报。确保正确计算闹钟时间。

请参阅 API:“注册的警报在设备休眠时保留(并且可以选择在该时间段内关闭时唤醒设备),但如果关闭并重新启动,则会被清除。” - http://developer.android.com/reference/android/app/AlarmManager。 html

Alarms will be cleared on reboot.

What you can do is

  1. persist alarm information in a db table
  2. register for the REBOOT_COMPLETED-Event
  3. On reboot, start a background thread the re-registers alarm. Make sure that you compute the alarm times correctly.

See API: "Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted." - http://developer.android.com/reference/android/app/AlarmManager.html

深府石板幽径 2024-11-07 15:08:42

一个 OnBootReceiver 类

public class OnBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        mgr.setRepeating(AlarmManager.RTC_WAKEUP,
        SystemClock.currentThreadTimeMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES,
                    pi); 

    }

}

创建

<receiver android:name=".OnBootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

在您的活动清单中

sendBroadcast(new Intent(this, OnBootReceiver.class));

Create a class OnBootReceiver

public class OnBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        mgr.setRepeating(AlarmManager.RTC_WAKEUP,
        SystemClock.currentThreadTimeMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES,
                    pi); 

    }

}

inside manifest

<receiver android:name=".OnBootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

inside your activity

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