Android - 即使应用程序未运行,也每15分钟运行一次后台任务

时间:2022-07-29 02:47:02

I need to build a background task that runs every 10/15 minutes (doesn't really matter, either is good), even when the application is not running.

我需要构建一个每10/15分钟运行一次的后台任务(并不重要,或者是好的),即使应用程序没有运行也是如此。

How can I accomplish this? I can't seem the wrap my head around this.

我怎么能做到这一点?我似乎无法绕过这个。

I read I could use some sort of runnable() functionality or use a background services or AlarmManager. I was thinking of a background service, since it also must be done when the application itself is not running.

我读过我可以使用某种runnable()功能或使用后台服务或AlarmManager。我在考虑后台服务,因为它也必须在应用程序本身未运行时完成。

What is a better way of doing this and how could I do it?

什么是更好的方法,我怎么能这样做?

4 个解决方案

#1


22  

You have have detemined the amount of time (interval) to execute a snippet of code, its better to use AlarmManager because its more energy effient. If your app needs to listen to some sort of a event , then Service is what you need.

你已经确定了执行一段代码的时间(间隔),最好使用AlarmManager,因为它更节能。如果您的应用需要收听某种类型的活动,那么服务就是您所需要的。

public static void registerAlarm(Context context) {
    Intent i = new Intent(context, YOURBROADCASTRECIEVER.class);

    PendingIntent sender = PendingIntent.getBroadcast(context,REQUEST_CODE, i, 0);

    // We want the alarm to go off 3 seconds from now.
    long firstTime = SystemClock.elapsedRealtime();
    firstTime += 3 * 1000;//start 3 seconds after first register.

    // Schedule the alarm!
    AlarmManager am = (AlarmManager) context
            .getSystemService(ALARM_SERVICE);
    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
            600000, sender);//10min interval

}

#2


2  

Alarm Manager (system service) vs Remote Service with inner alarm implementation (separate process)?

报警管理器(系统服务)与具有内部报警实施的远程服务(单独的流程)?

Alarm Manager is your choice, because it already has what you need, you just have to set alarm intervals

警报管理器是您的选择,因为它已经满足您的需求,您只需设置警报间隔即可

#3


1  

You can also achieve this via a SyncAdapter Here's a sample for your to look at and get inspired

您也可以通过SyncAdapter实现这一目标。这里有一个示例供您查看并获得灵感

SyncAdapter sample

SyncAdapter示例

#4


0  

The best approach was introduced at Google I/O 2018 - WorkManager.

最好的方法是在Google I / O 2018 - WorkManager中引入的。

You can find documentation here.

你可以在这里找到文档。

#1


22  

You have have detemined the amount of time (interval) to execute a snippet of code, its better to use AlarmManager because its more energy effient. If your app needs to listen to some sort of a event , then Service is what you need.

你已经确定了执行一段代码的时间(间隔),最好使用AlarmManager,因为它更节能。如果您的应用需要收听某种类型的活动,那么服务就是您所需要的。

public static void registerAlarm(Context context) {
    Intent i = new Intent(context, YOURBROADCASTRECIEVER.class);

    PendingIntent sender = PendingIntent.getBroadcast(context,REQUEST_CODE, i, 0);

    // We want the alarm to go off 3 seconds from now.
    long firstTime = SystemClock.elapsedRealtime();
    firstTime += 3 * 1000;//start 3 seconds after first register.

    // Schedule the alarm!
    AlarmManager am = (AlarmManager) context
            .getSystemService(ALARM_SERVICE);
    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
            600000, sender);//10min interval

}

#2


2  

Alarm Manager (system service) vs Remote Service with inner alarm implementation (separate process)?

报警管理器(系统服务)与具有内部报警实施的远程服务(单独的流程)?

Alarm Manager is your choice, because it already has what you need, you just have to set alarm intervals

警报管理器是您的选择,因为它已经满足您的需求,您只需设置警报间隔即可

#3


1  

You can also achieve this via a SyncAdapter Here's a sample for your to look at and get inspired

您也可以通过SyncAdapter实现这一目标。这里有一个示例供您查看并获得灵感

SyncAdapter sample

SyncAdapter示例

#4


0  

The best approach was introduced at Google I/O 2018 - WorkManager.

最好的方法是在Google I / O 2018 - WorkManager中引入的。

You can find documentation here.

你可以在这里找到文档。