JobScheduler在条件满足要求时不会重新启动

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

JobScheduler won't restart when condition meets the requirements

问题

以下是翻译好的内容:

我已经将我的JobScheduler设置为在手机充电并连接到 WiFi 时启动。但是当我关闭 WiFi 时,它会显示作业已取消,但当我重新打开 WiFi 时,它不会重新启动。以下是我的代码:

MainActivity.java

  1. private static final String TAG = "MainActivity";
  2. public void scheduleJob(View v) {
  3. ComponentName componentName = new ComponentName(this, ExampleJobService.class);
  4. JobInfo info = new JobInfo.Builder(123, componentName)
  5. .setRequiresCharging(true)
  6. .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
  7. .setPersisted(true)
  8. .setPeriodic(15 * 60 * 1000)
  9. .build();
  10. JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
  11. int resultCode = scheduler.schedule(info);
  12. if (resultCode == JobScheduler.RESULT_SUCCESS) {
  13. Log.d(TAG, "Job scheduled");
  14. } else {
  15. Log.d(TAG, "Job scheduling failed");
  16. }
  17. }
  18. public void cancelJob(View v){
  19. JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
  20. scheduler.cancel(123);
  21. Log.d(TAG, "Job cancelled");
  22. }

ExampleJobService.java

  1. private static final String TAG = "ExampleJobService";
  2. private boolean jobCanceled = false;
  3. @Override
  4. public boolean onStartJob(JobParameters params) {
  5. Log.d(TAG, "Job started");
  6. doBackgroundWork(params);
  7. return true;
  8. }
  9. private void doBackgroundWork(final JobParameters params) {
  10. new Thread(new Runnable() {
  11. @Override
  12. public void run() {
  13. for (int i = 0; i < 10; i++) {
  14. Log.d(TAG, "run: " + i);
  15. if (jobCanceled) {
  16. return;
  17. }
  18. try {
  19. Thread.sleep(1000);
  20. } catch (InterruptedException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. Log.d(TAG, "Job finished");
  25. jobFinished(params, false);
  26. }
  27. }).start();
  28. }
  29. @Override
  30. public boolean onStopJob(JobParameters params) {
  31. Log.d(TAG, "Job cancelled before completion");
  32. jobCanceled = true;
  33. return true;
  34. }

Manifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.hoversfw.test">
  4. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:roundIcon="@mipmap/ic_launcher_round"
  10. android:supportsRtl="true"
  11. android:theme="@style/AppTheme">
  12. <activity android:name=".MainActivity">
  13. <intent-filter>
  14. <action android:name="android.intent.action.MAIN" />
  15. <category android:name="android.intent.category.LAUNCHER" />
  16. </intent-filter>
  17. </activity>
  18. <service android:name=".ExampleJobService"
  19. android:permission="android.permission.BIND_JOB_SERVICE"/>
  20. </application>
  21. </manifest>

当我关闭 WiFi,安排任务,终止应用进程,然后打开 WiFi,JobScheduler 会启动并在 Logcat 中显示。此外,当我安排任务并启动它后,然后终止应用,Logcat 中的打印信息就会停止。因此,我认为每当 JobScheduler 被中断时,它就不会重新启动。我已经添加了所需的权限,并且 minSDK 是 21。所以关于权限应该没有问题。请帮助。

英文:

I have set my JobScheduler to start when phone is charging and connected to wifi. But when I turn off wifi it says job cancelled, but when I turn it back on it won't restart. Here's my code:<br><br>MainActivity.java

  1. private static final String TAG=&quot;MainActivity&quot;;
  2. public void scheduleJob(View v) {
  3. ComponentName componentName = new ComponentName(this, ExampleJobService.class);
  4. JobInfo info = new JobInfo.Builder(123, componentName)
  5. .setRequiresCharging(true)
  6. .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
  7. .setPersisted(true)
  8. .setPeriodic(15 * 60 * 1000)
  9. .build();
  10. JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
  11. int resultCode = scheduler.schedule(info);
  12. if (resultCode == JobScheduler.RESULT_SUCCESS) {
  13. Log.d(TAG, &quot;Job scheduled&quot;);
  14. } else {
  15. Log.d(TAG, &quot;Job scheduling failed&quot;);
  16. }
  17. }
  18. public void cancelJob(View v){
  19. JobScheduler scheduler=(JobScheduler)getSystemService(JOB_SCHEDULER_SERVICE);
  20. scheduler.cancel(123);
  21. Log.d(TAG,&quot;Job cancelled&quot;);
  22. }

<br>
ExampleJobService.java

  1. private static final String TAG=&quot;ExampleJobService&quot;;
  2. private boolean jobCanceled=false;
  3. @Override
  4. public boolean onStartJob(JobParameters params) {
  5. Log.d(TAG, &quot;Job started&quot;);
  6. doBackgroundWork(params);
  7. return true;
  8. }
  9. private void doBackgroundWork(final JobParameters params) {
  10. new Thread(new Runnable() {
  11. @Override
  12. public void run() {
  13. for (int i = 0; i &lt; 10; i++) {
  14. Log.d(TAG, &quot;run: &quot; + i);
  15. if (jobCanceled) {
  16. return;
  17. }
  18. try {
  19. Thread.sleep(1000);
  20. } catch (InterruptedException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. Log.d(TAG, &quot;Job finished&quot;);
  25. jobFinished(params, false);
  26. }
  27. }).start();
  28. }
  29. @Override
  30. public boolean onStopJob(JobParameters params) {
  31. Log.d(TAG, &quot;Job cancelled before completion&quot;);
  32. jobCanceled = true;
  33. return true;
  34. }

Manifest.xml

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. package=&quot;com.hoversfw.test&quot;&gt;
  4. &lt;uses-permission android:name=&quot;android.permission.RECEIVE_BOOT_COMPLETED&quot; /&gt;
  5. &lt;application
  6. android:allowBackup=&quot;true&quot;
  7. android:icon=&quot;@mipmap/ic_launcher&quot;
  8. android:label=&quot;@string/app_name&quot;
  9. android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
  10. android:supportsRtl=&quot;true&quot;
  11. android:theme=&quot;@style/AppTheme&quot;&gt;
  12. &lt;activity android:name=&quot;.MainActivity&quot;&gt;
  13. &lt;intent-filter&gt;
  14. &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
  15. &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
  16. &lt;/intent-filter&gt;
  17. &lt;/activity&gt;
  18. &lt;service android:name=&quot;.ExampleJobService&quot;
  19. android:permission=&quot;android.permission.BIND_JOB_SERVICE&quot;/&gt;
  20. &lt;/application&gt;
  21. &lt;/manifest&gt;

When I turn off wifi, schedule job, kill app process, and turn wifi on, the JobScheduler starts and I see it printing in the Logcat. Also, when I schedule it and it starts, then kill app, it just stopped printing in Logcat. So I think whenever the JobScheduler got interrupted, it just won't restart. I have permissions added, and minSDK is 21. So there should be no problem about permissions. Help

答案1

得分: 1

JobScheduler在调用jobFinished(params, true)时将会重新启动。您可以在返回之前添加此操作。

  1. if (jobCanceled) {
  2. jobFinished(params, true);
  3. return;
  4. }
英文:

JobSchleduler will be restart when you call jobFinished(params, true). You can add it before return

  1. if (jobCanceled) {
  2. jobFinished(params, true);
  3. return;
  4. }

huangapple
  • 本文由 发表于 2020年5月3日 12:34:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/61569683.html
匿名

发表评论

匿名网友

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

确定