gpt4 book ai didi

具有空闲要求的 Android JobScheduler 不会被解雇

转载 作者:行者123 更新时间:2023-11-29 15:40:14 25 4
gpt4 key购买 nike

我有一个 service 扩展 JobService 如下:

public class MyService extends JobService {
private int notificationNumber = 0;

private Thread thread;


@Override
public boolean onStartJob(JobParameters params) {
Toast.makeText(this, "Service1 Started with JobId: " + params.getJobId(), Toast.LENGTH_LONG).show();
thread = new WorkerThread(this, params);
thread.start();
return true;
}

@Override
public boolean onStopJob(JobParameters params) {
createNotification("Service1 onStop with JobId");
return false;
}

public void createNotification(String msg) {
notificationNumber++;
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentText(msg);
builder.setContentTitle("New Notification #" + notificationNumber);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setAutoCancel(true);

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = builder.build();
manager.notify(notificationNumber, notification);
}

}

class WorkerThread extends Thread {
private MyService service;
private JobParameters params;
public WorkerThread(MyService service, JobParameters params) {
this.service = service;
this.params = params;
}

@Override
public void run() {
try {
service.createNotification("Service1.WorkerThread Started");
Thread.sleep(10000);

service.createNotification("Service1.WorkerThread Finished");
} catch (Exception e) {
e.printStackTrace();
} finally {
Log.i("##########", "Job Finished");
service.jobFinished(params, false);
}
}
}

并使用 JobInfo.builder 我从中实例化并通过 JobScheduler 运行它,如下所示:

public void scheduleService() {
int JOB_ID =1;
long y=100;
ComponentName serviceName = new ComponentName(this, MyService.class);
JobInfo jobInfo = new JobInfo.Builder(JOB_ID, serviceName)
// .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
// .setBackoffCriteria(y,JobInfo.BACKOFF_POLICY_LINEAR)
.setPeriodic(y)
.setRequiresDeviceIdle(true)
.build();
JobScheduler scheduler = (JobScheduler) this.getSystemService(Context.JOB_SCHEDULER_SERVICE);
int result = scheduler.schedule(jobInfo);
if (result == JobScheduler.RESULT_SUCCESS) {
Log.d("Test", "Job scheduled successfully!");
}
}

即使我使用:

        JobInfo jobInfo = new JobInfo.Builder(JOB_ID, serviceName)
// .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
// .setBackoffCriteria(y,JobInfo.BACKOFF_POLICY_LINEAR)
.setPeriodic(y)
// .setRequiresDeviceIdle(true)
.build();

没有任何作用。虽然这样:

if (result == JobScheduler.RESULT_SUCCESS) {
Log.d("Test", "Job scheduled successfully!");
}

显示作业已正确安排,但即使有 1 行服务也不会被调用!有什么想法吗?

关于 list :

       <service
android:name=".MyService"
android:enabled="true"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="true"></service>

一切似乎都是对的!!!

===========编辑没有空闲要求会被解雇,但要经过很长时间。

TG.

最佳答案

终于找到原因了。一切正常,但最短时间。

我在这里找到了问题的答案:

Job Scheduler not running on Android N

正如您在此处看到的,为计划设置的最短时间约为 15 分钟。我让手机处于空闲模式 15 分钟,然后它被触发了。

我不关心的对应日志是这样的:

01-19 22:39:01.775 31125-31125/ir.cclever.prepareforclass W/JobInfo: Specified interval for 114 is +10ms. Clamped to +15m0s0ms

我希望这篇文章能帮助其他遇到此问题的人。

TG.

关于具有空闲要求的 Android JobScheduler 不会被解雇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41749740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com