为什么每秒要调用Job服务的onStartJob方法?

时间:2019-03-29 15:52:11

标签: android

我有一个计划工作,计划每30分钟执行一次。我正在使用AsyncHTTPClient做一些与网络有关的事情。收到响应后,我调用JobFinished()。但是我可以看到有时onStart方法每秒调用一次,而不是每30分钟调用一次

下面是我的Jobscheduler课

公共类MJobScheduler扩展JobService {

BlockingQueue<Request> blockingQueue;

PrefManager prefManager;
String url;
private static final String TAG = "MJobScheduler";


//when job is scheduled, this method is called
@Override
public boolean onStartJob(JobParameters jobParameters) {
    prefManager = new PrefManager(getApplicationContext());
    Request request = null;


    if (!MyUtility.isNetworkAvailable(getApplicationContext())) {
        Log.d("no network", "so closing");
        jobFinished(jobParameters, false);
        return false;
    }


    blockingQueue = prefManager.getFileBlockingQueue();
    //Log.d("file q size", String.valueOf(blockingQueue.size()));
    if (blockingQueue.size() <= 0) {
        Log.d("no size", "so returning");
        jobFinished(jobParameters, false);
        return false;
    }

    request = blockingQueue.peek();

    RequestParams params = new RequestParams();

    formParameters(request,params);

    Log.d("url",url);
    AsyncHttpClient client = new AsyncHttpClient();
    client.setTimeout(30000);
    client.setResponseTimeout(30000);
    client.post(url, params,
            new TextHttpResponseHandler() {
                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

              //      Log.d("fail", responseString);
                   // Log.d("code", String.valueOf(statusCode));
                    jobFinished(jobParameters, false);
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {

                    String filename = findFileNameFromResponse(responseString);
                    Log.d("filename is:",filename+"this");
                    if (deleteTheFile(filename)) {
                        Log.d("file ", req.getFileString() + " successfully deleted");
                    }


                    Log.d("response",responseString);

                    jobFinished(jobParameters, false);
                    Log.d("job is","finished");
                }
            });

    return true;

}


// this method will be called when there is any interruption while job execution
@Override
public boolean onStopJob(JobParameters jobParameters) {

    return false; // if  want to reschedule then return job
}

}

我正在按如下计划工作。

    ComponentName componentName = new ComponentName(this, MJobScheduler.class);
    JobInfo.Builder builder = new JobInfo.Builder(1, componentName);
    builder.setPeriodic(30 * 60 * 1000);
    builder.setPersisted(true);
    jobInfo = builder.build();

    jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
    jobScheduler.schedule(jobInfo);

0 个答案:

没有答案