Android 使作业运行并每2分钟执行一次池操作,无论应用程序是否已关闭或已从内存中删除

Android 使作业运行并每2分钟执行一次池操作,无论应用程序是否已关闭或已从内存中删除,android,android-service,android-jobscheduler,android-priority-jobqueue,Android,Android Service,Android Jobscheduler,Android Priority Jobqueue,我正在使用android优先级作业队列“com.github.yigit:android优先级作业队列”。确保每2分钟运行一次。下面是代码 public PoolingJob () { // This job requires network connectivity, // and should be persisted in case the application exits before job is completed. super (new Params (

我正在使用android优先级作业队列“com.github.yigit:android优先级作业队列”。确保每2分钟运行一次。下面是代码

public PoolingJob () {

    // This job requires network connectivity,
    // and should be persisted in case the application exits before job is completed.
    super (new Params (PRIORITY).requireNetwork ().groupBy (Const.POOLING_QUEUE_GROUP)
            .delayInMs (120000).persist ());//120 sec delay
}
但只要应用程序关闭或从内存中删除,它就会被挂起。 当应用程序关闭或从内存中删除时,如何使作业连续运行并每2分钟进行一次池化。有没有像粘性服务或类似的服务

构建服务器池系统。需要上面的API 15。

您可以使用以给定间隔发送
意图
s,并在
广播接收器
中运行作业,无论应用程序是否正在运行


也就是说,两分钟的时间间隔很短,您应该避免执行任何经常执行的任务(尤其是与网络相关的任务)。这将大大增加电池电量。

使用
AlarmManager
Service
可以实现这一点。 请阅读此处的文档

下面是设置和删除重复任务警报的代码块

设置警报

public void createAlarm() {
        ServerDetails serverDetails = new ServerDetails(this);
        if (serverDetails.isSettingsOK()) {
            Intent i = new Intent(this, MyService.class);
            if (PendingIntent.getService(this, 0, i,
                    PendingIntent.FLAG_NO_CREATE) == null) {
                PendingIntent pendingIntent = PendingIntent.getService(this, 0,
                        i, PendingIntent.FLAG_CANCEL_CURRENT);
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                int interval = 2 * 60 * 1000;
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                        System.currentTimeMillis() + interval, interval,
                        pendingIntent);
                Log.d("alarm", "alarm set successfully " + interval);
            } else {
                Log.d("alarm", "alarm already set");
            }

        }

    }
取消警报

public void cancelAlarm() {
    Intent i = new Intent(this, MyService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, i,
            PendingIntent.FLAG_NO_CREATE);
    if (pendingIntent != null) {
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);
        pendingIntent.cancel();
        Log.d("alarm", "alarm cancelled");
    }
}
这是警报激活时将在后台运行的服务

public class MyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }

    @Override
    public void onCreate() {

        super.onCreate();
        Log.d("service Created", "service created");

    }


    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
       doYourTaskHere();
    }

    private void doYourTaskHere() {

        // call here webservice or any other task here which you want to do in every two minutes

    }


}
你为什么要这么做 如果我是你的话,我会转而研究SyncAdapters。您可以将它们设计为即使在后台也能工作,而且在架构上比处理AlarmManager更好。 在这里查看它们。 它们的电池性能更好。允许身份验证,并向代码库添加插件体系结构