Android - 立即启动闹钟服务?

发布于 2024-12-21 03:51:36 字数 675 浏览 3 评论 0原文

我创建了一个启动接收器,每 5 分钟重复调用一次唤醒意图服务,但无法弄清楚如何在安装应用程序后立即启动该服务..?我不想依赖用户在设备开始运行之前重新启动设备!

到目前为止,这是我的代码:

public class OnBootReceiver extends BroadcastReceiver {
private static final int PERIOD = 300000; // check every 5 minutes

@Override
public void onReceive(Context context, Intent intent) {
    AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, OnAlarmReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);

    mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 60000, PERIOD, pi);
}}

有人可以帮我吗? :)

I have created an On Boot Receiver to repeatedly call a wakeful intent service every 5 minutes but cannot figure out how to start the service immediately when the app is installed..? I do not want to rely on the user rebooting their device before it starts to run!

Here is my code so far :

public class OnBootReceiver extends BroadcastReceiver {
private static final int PERIOD = 300000; // check every 5 minutes

@Override
public void onReceive(Context context, Intent intent) {
    AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, OnAlarmReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);

    mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 60000, PERIOD, pi);
}}

Can anyone help me out pls? :)

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

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

发布评论

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

评论(2

夜血缘 2024-12-28 03:51:36

如果您想设置警报管理器在安装应用程序时启动您的服务,那么这是不可能的。如果你愿意的话,这是操作系统的限制,安全性。但如果你想在应用程序启动的那一刻启动该服务,只需调用它,它就会继续运行。

If you want to set an alarmmanager to start your service when the app is installed, then it's not possible. It's a OS limitation, security if you will. But if you want to start the service in the moment the app starts, just call it, it will keep runing.

凯凯我们等你回来 2024-12-28 03:51:36

本质上,由于 Application 对象是在应用程序启动时以及接收到 BOOT_COMPLETED Intent 时创建的,因此您可以在自定义 Application 类的 onCreate 方法中向 AlarmManager 注册。请注意,每次进程启动时都会实例化 Application 对象,其中包括临时终止进程以节省资源的情况。但如果你不以任何方式改变PendingIntent,那么一遍又一遍地注册应该没有问题。

但是,安装后无法启动应用程序,必须首先进行一些用户交互。

Essentially, since the Application object is created when the application is started and when the BOOT_COMPLETED Intent is received, you could register with the AlarmManager in the onCreate method in your custom Application class. Just be aware that the Application object is instantiated every time the process starts, which includes cases where the process is temporarily killed to save resources. But if you don't change the PendingIntent in any way, it should be no problem to register over and over again.

However, it is not possible to start the application when it is installed, there has to be some user interaction first.

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