JobScheduler 没有每分钟都安排服务

JobScheduler not scheduling service in every minute

提问人:D.J 提问时间:11/11/2023 更新时间:11/11/2023 访问量:9

问:

我想安排在 1 分钟内开始的作业,并按相同的时间间隔继续。我尝试了以下方法,但没有成功。

public static void scheduleJob(Context context) {
    ComponentName serviceComponent = new ComponentName(context, MyJobService.class);
    JobInfo.Builder builder = new JobInfo.Builder(0, serviceComponent);
    builder.setMinimumLatency(60 * 1000); // Wait at least 30s
    builder.setOverrideDeadline(60 * 1000); // Maximum delay 60s

    JobScheduler jobScheduler = (JobScheduler)context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
    jobScheduler.schedule(builder.build());
}

public class MyJobService extends JobService {
@Override
public boolean onStartJob(JobParameters params) {
    // Write your code here
    Log.d("KKKKKKKK MyJobService", "onStartJob");
    return true;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d("KKKKKKKK onStartCommand", "onStartCommand");
    return super.onStartCommand(intent, flags, startId);
}
}


public class MyApplication extends Application {
@Override
public void onCreate() {
    super.onCreate();
    scheduleJob(getApplicationContext());
}
}


 <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
 <service
        android:name=".service.MyJobService"
        android:label="Word service"
        android:permission="android.permission.BIND_JOB_SERVICE" >
 </service>
Android 服务 android-jobscheduler

评论


答: 暂无答案