警报和作业在应用关闭时是否被清除?

huangapple go评论81阅读模式
英文:

Alarms and jobs being cleared when application is closed?

问题

以下是您要翻译的内容:


So, I am trying to run a service in the background that can send some app data to my server
So far I've tried using AlarmManager, JobScheduler classes to run the service periodically but its no use since any alarms created and jobs scheduled ARE CLEARED as the application is closed (closed here does not mean force stopped). I looked around and have seen a lot of StackOverflow questions for similar usecases(but older android versions) and same clearing problem, but no concrete answer or implementation i found is working. (is this an issue with latest versions of android?)

My use case is small data(in/ kbs) upload once or twice a day from the application to maintain usage track of the application.

Since i can't expect user to keep his app open all day...

  • How to achieve this periodicity as explained ?


UPDATE-1

According to @Kabumere answer and some help from android dev docs i used WorkManager and tried this code below
Expected behaviour(for now)
continous repeating logs with 20 min interval (20 min is just for testing) regardless if app was closed(ie that 30 min interval between 6 and 7)
Actual Behaviour

  1. app is in focus on testing device(genymotion android 9)
  2. 08:47 onCreate schedules first job and it runs
  3. app is closed but present in recents
  4. 09:02 second job runs (15 min apart)
  5. app now closed from recent
  6. // no further logs for 30 mins, jobs not started
  7. 09:36 application started manually, nothing is enqueued as per logic
  8. jobs continue at 15 min interval
    2020-08-05 08:47:53.260 7746-7746/com.example.workmanagerapi D/debugTag: Scheduling 2020-08-05 08:47:53.655 7746-7802/com.example.workmanagerapi D/debugTag: DOING SOME WORK !!! 2020-08-05 09:02:53.732 7746-8007/com.example.workmanagerapi D/debugTag: DOING SOME WORK !!! 2020-08-05 09:36:47.165 8347-8347/com.example.workmanagerapi D/debugTag: Already Scheduled 2020-08-05 09:36:47.193 8347-8376/com.example.workmanagerapi D/debugTag: DOING SOME WORK !!! 2020-08-05 09:51:47.269 8347-8486/com.example.workmanagerapi D/debugTag: DOING SOME WORK !!!

MainActivity.java


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SharedPreferences SP = getSharedPreferences("prefs",MODE_PRIVATE);
        SharedPreferences.Editor editor = SP.edit();
        boolean enqueue = SP.getBoolean("enqueue", false);
        if(enqueue) {
            Log.d("debugTag", "Already Scheduled");
        }
        else {
            Log.d("debugTag", "Scheduling");
            doWork();
            editor.putBoolean("enqueue",true);
            editor.apply();
        }
    }

    private void doWork() {
        PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 1, TimeUnit.MINUTES);
        // i read on docs that this time limit will default to (5+15) minutes minimum
        PeriodicWorkRequest myWork = myWorkBuilder.build();
        WorkManager.getInstance(this).enqueue(myWork);
    }
} ```

MyWorker.java
``` public class MyWorker extends Worker {
    public MyWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        Log.d("debugTag", "DOING SOME WORK !!!");
        return Result.success();
    }
英文:

So, I am trying to run a service in the background that can send some app data to my server
So far I've tried using AlarmManager, JobScheduler classes to run the service periodically but its no use since any alarms created and jobs scheduled ARE CLEARED as the application is closed (closed here does not mean force stopped). I looked around and have seen a lot of StackOverflow questions for similar usecases(but older android versions) and same clearing problem, but no concrete answer or implementation i found is working. (is this an issue with latest versions of android?)

My use case is small data(in/ kbs) upload once or twice a day from the application to maintain usage track of the application.

Since i can't expect user to keep his app open all day...

  • How to achieve this periodicity as explained ?


UPDATE-1

According to @Kabumere answer and some help from android dev docs i used WorkManager and tried this code below
Expected behaviour(for now)
continous repeating logs with 20 min interval (20 min is just for testing) regardless if app was closed(ie that 30 min interval between 6 and 7)
Actual Behaviour

  1. app is in focus on testing device(genymotion android 9)
  2. 08:47 onCreate schedules first job and it runs
  3. app is closed but present in recents
  4. 09:02 second job runs (15 min apart)
  5. app now closed from recent
  6. // no further logs for 30 mins, jobs not started
  7. 09:36 application started manually, nothing is enqueued as per logic
  8. jobs continue at 15 min interval
2020-08-05 08:47:53.260 7746-7746/com.example.workmanagerapi D/debugTag: Scheduling
2020-08-05 08:47:53.655 7746-7802/com.example.workmanagerapi D/debugTag: DOING SOME WORK !!!
2020-08-05 09:02:53.732 7746-8007/com.example.workmanagerapi D/debugTag: DOING SOME WORK !!!
2020-08-05 09:36:47.165 8347-8347/com.example.workmanagerapi D/debugTag: Already Scheduled
2020-08-05 09:36:47.193 8347-8376/com.example.workmanagerapi D/debugTag: DOING SOME WORK !!!
2020-08-05 09:51:47.269 8347-8486/com.example.workmanagerapi D/debugTag: DOING SOME WORK !!!

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SharedPreferences SP = getSharedPreferences("prefs",MODE_PRIVATE);
        SharedPreferences.Editor editor = SP.edit();
        boolean enqueue = SP.getBoolean("enqueue", false);
        if(enqueue) {
            Log.d("debugTag", "Already Scheduled");
        }
        else {
            Log.d("debugTag", "Scheduling");
            doWork();
            editor.putBoolean("enqueue",true);
            editor.apply();
        }
    }

    private void doWork() {
        PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 1, TimeUnit.MINUTES);
        // i read on docs that this time limit will default to (5+15) minutes minimum
        PeriodicWorkRequest myWork = myWorkBuilder.build();
        WorkManager.getInstance(this).enqueue(myWork);
    }
}

MyWorker.java

public class MyWorker extends Worker {
    public MyWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        Log.d("debugTag", "DOING SOME WORK !!!");
        return Result.success();
    }

答案1

得分: 1

我建议使用WorkManager API而不是创建一个服务。

使用WorkManager,您可以排队一个PeriodicWorkRequest,它将按照您在其Builder构造函数中设置的间隔运行(例如每24小时运行一次)。通过WorkManager安排的作业在应用关闭时不应删除,将在不同事件(如设备重启)时自动重新安排,并且甚至可以在应用处于后台时运行。

您还可以在WorkRequests上设置约束,以便指定诸如“仅在我们具有任何类型的数据连接时尝试运行此作业”(setRequiredNetworkType(NetworkType.CONNECTED))之类的事项。这对于您需要发送数据的用例非常有用,这将需要一些连接。

英文:

I'd recommend using the WorkManager API over creating a Service.

With WorkManager you can enqueue a PeriodicWorkRequest that will run at the interval you set in its Builder constructor (like run every 24 hours). Scheduled jobs through WorkManager should not be deleted when the app is closed, will be automatically rescheduled at different events like device reboot, and can run even when the app is in the background.

You can also set Constraints on WorkRequests so you can specify things like "only attempt to run this job if we have any sort of data connection" (setRequiredNetworkType(NetworkType.CONNECTED)). This is good for your use case of needing to send data, which will require some connection.

huangapple
  • 本文由 发表于 2020年8月5日 08:15:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63256702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定