英文:
JobScheduler won't restart when condition meets the requirements
问题
以下是翻译好的内容:
我已经将我的JobScheduler
设置为在手机充电并连接到 WiFi 时启动。但是当我关闭 WiFi 时,它会显示作业已取消,但当我重新打开 WiFi 时,它不会重新启动。以下是我的代码:
MainActivity.java
private static final String TAG = "MainActivity";
public void scheduleJob(View v) {
ComponentName componentName = new ComponentName(this, ExampleJobService.class);
JobInfo info = new JobInfo.Builder(123, componentName)
.setRequiresCharging(true)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
.setPersisted(true)
.setPeriodic(15 * 60 * 1000)
.build();
JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
int resultCode = scheduler.schedule(info);
if (resultCode == JobScheduler.RESULT_SUCCESS) {
Log.d(TAG, "Job scheduled");
} else {
Log.d(TAG, "Job scheduling failed");
}
}
public void cancelJob(View v){
JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
scheduler.cancel(123);
Log.d(TAG, "Job cancelled");
}
ExampleJobService.java
private static final String TAG = "ExampleJobService";
private boolean jobCanceled = false;
@Override
public boolean onStartJob(JobParameters params) {
Log.d(TAG, "Job started");
doBackgroundWork(params);
return true;
}
private void doBackgroundWork(final JobParameters params) {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
Log.d(TAG, "run: " + i);
if (jobCanceled) {
return;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.d(TAG, "Job finished");
jobFinished(params, false);
}
}).start();
}
@Override
public boolean onStopJob(JobParameters params) {
Log.d(TAG, "Job cancelled before completion");
jobCanceled = true;
return true;
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hoversfw.test">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".ExampleJobService"
android:permission="android.permission.BIND_JOB_SERVICE"/>
</application>
</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
private static final String TAG="MainActivity";
public void scheduleJob(View v) {
ComponentName componentName = new ComponentName(this, ExampleJobService.class);
JobInfo info = new JobInfo.Builder(123, componentName)
.setRequiresCharging(true)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
.setPersisted(true)
.setPeriodic(15 * 60 * 1000)
.build();
JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
int resultCode = scheduler.schedule(info);
if (resultCode == JobScheduler.RESULT_SUCCESS) {
Log.d(TAG, "Job scheduled");
} else {
Log.d(TAG, "Job scheduling failed");
}
}
public void cancelJob(View v){
JobScheduler scheduler=(JobScheduler)getSystemService(JOB_SCHEDULER_SERVICE);
scheduler.cancel(123);
Log.d(TAG,"Job cancelled");
}
<br>
ExampleJobService.java
private static final String TAG="ExampleJobService";
private boolean jobCanceled=false;
@Override
public boolean onStartJob(JobParameters params) {
Log.d(TAG, "Job started");
doBackgroundWork(params);
return true;
}
private void doBackgroundWork(final JobParameters params) {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
Log.d(TAG, "run: " + i);
if (jobCanceled) {
return;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.d(TAG, "Job finished");
jobFinished(params, false);
}
}).start();
}
@Override
public boolean onStopJob(JobParameters params) {
Log.d(TAG, "Job cancelled before completion");
jobCanceled = true;
return true;
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hoversfw.test">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".ExampleJobService"
android:permission="android.permission.BIND_JOB_SERVICE"/>
</application>
</manifest>
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)
时将会重新启动。您可以在返回之前添加此操作。
if (jobCanceled) {
jobFinished(params, true);
return;
}
英文:
JobSchleduler will be restart when you call jobFinished(params, true)
. You can add it before return
if (jobCanceled) {
jobFinished(params, true);
return;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论