有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用jobscheduler将jobworkitem排队

安卓系统中的JobWorkItem在互联网上几乎没有实例

尝试在Android中向作业添加工作(使用JobScheduler框架)。但我的工作意向从未被解雇。试图使用this方法使其特定。我注意到我的服务中的断点(JsonDownloadIntentService)从未命中

MainActivity.java

private void startJobService() {
        JobScheduler jobScheduler = (JobScheduler) getApplicationContext().getSystemService(Context.JOB_SCHEDULER_SERVICE);
        jobScheduler.schedule(getJobInfo());
    }

    private JobInfo getJobInfo() {
        if(jobInfo == null) {
           jobInfo =  (new JobInfo.Builder(10, new ComponentName(getApplicationContext().getPackageName(), EmptyJobService.class.getName()))).setMinimumLatency(0).setOverrideDeadline(5000).build();
        }
        return jobInfo;
    }

    private void enqueueWorkItem() {
        Intent startJsonDownloadService = new Intent(this, JsonDownloadIntentService.class);
        ((JobScheduler)getApplicationContext().getSystemService(Context.JOB_SCHEDULER_SERVICE)).enqueue(getJobInfo(), new JobWorkItem(startJsonDownloadService));
    }

清空作业服务。爪哇

        public class EmptyJobService extends JobService {
        @Override
        public boolean onStartJob(JobParameters params) {
            Log.d(Constants.JOB_SERVICE, "Job Service started");
            Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT).show();
            return false;
        }

        @Override
        public boolean onStopJob(JobParameters params) {
            Log.d(Constants.JOB_SERVICE, "Job service stopped");
            return false;
        }
}

AndroidManifest。xml

<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    package="com.viewer.myphotoviewer">
    <uses-permission 安卓:name="安卓.permission.INTERNET" />
   <application
        安卓:allowBackup="true"
        安卓:icon="@mipmap/ic_launcher"
        安卓:label="@string/app_name"
        安卓:roundIcon="@mipmap/ic_launcher_round"
        安卓:supportsRtl="true"
        安卓:theme="@style/AppTheme">


        <activity 安卓:name=".PhotoGalleryActivity">
            <intent-filter>
                <action 安卓:name="安卓.intent.action.MAIN" />
                <category 安卓:name="安卓.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service 安卓:name=".ImageDownloadIntentService" />
        <service 安卓:name=".JsonDownloadIntentService" />
        <service 安卓:name=".EmptyJobService"
            安卓:permission="安卓.permission.BIND_JOB_SERVICE"
            安卓:exported="true"
            />

        <activity 安卓:name=".ImageViewActivity"></activity>
    </application>
</manifest>

共 (2) 个答案

  1. # 1 楼答案

    当您从onStartJob()返回false时,意味着您对系统说,一旦此方法返回“我的作业已完成”。所以android框架代表您启动onJobFinished()。 但是当您返回true时,这意味着您的作业需要超出此回调,并且您有责任在工作完成时调用onJobFinished

    您需要使用接收到的JobParameters的dequeue()并获取已排队的工作项。工作完成后,调用completeWork(工作项)。 处理完所有工作后,最后调用dequeue,这样系统就可以通过代表您调用jobFinished()来结束作业服务

    请阅读我撰写的解释JobWorkItem API的这篇文章以及JobWorkItem的工作示例

    https://medium.com/@kiitvishal89/jobworkitem-jobschedulers-way-of-splitting-your-job-d6d5385390b

  2. # 2 楼答案

    它是否与许可一起在清单中声明

    <service
    android:name=".MyJobService"
    android:permission="android.permission.BIND_JOB_SERVICE"/>