当前位置:  开发笔记 > 编程语言 > 正文

JobScheduler.schedule(作业)未启动JobService

如何解决《JobScheduler.schedule(作业)未启动JobService》经验,为你挑选了1个好方法。

我正在使用一个用于JobScheduler 的lib来处理棒棒糖前设备.我正在使用一个棒棒糖设备.问题是mJobScheduler.schedule(作业)应该启动我的工作服务,但它没有启动.此外,当我包含.setPeriodic(2000)方法时,应用程序崩溃了

堆栈跟踪.

08-06 00:37:06.729 19693-19693/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: rishabh.example.com.myapplication, PID: 19693
                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{rishabh.example.com.myapplication/rishabh.example.com.myapplication.MainActivity}: java.lang.IllegalArgumentException: Can't call setOverrideDeadline() on a periodic job.
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
                                                   at android.app.ActivityThread.access$800(ActivityThread.java:155)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:135)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
                                                Caused by: java.lang.IllegalArgumentException: Can't call setOverrideDeadline() on a periodic job.
                                                   at me.tatarka.support.job.JobInfo$Builder.build(JobInfo.java:477)
                                                   at rishabh.example.com.myapplication.MainActivity.constructJob(MainActivity.java:30)
                                                   at rishabh.example.com.myapplication.MainActivity.onCreate(MainActivity.java:19)
                                                   at android.app.Activity.performCreate(Activity.java:6010)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) 
                                                   at android.app.ActivityThread.access$800(ActivityThread.java:155) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                   at android.os.Looper.loop(Looper.java:135) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5343) 
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 

我的主要活动

public class MainActivity extends AppCompatActivity {

JobScheduler mJobScheduler;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mJobScheduler=JobScheduler.getInstance(this);
    constructJob();
}

private void constructJob() {

    JobInfo job = new JobInfo.Builder(0 /*jobid*/, new ComponentName(this,MyService.class))
            .setMinimumLatency(1000)
            .setOverrideDeadline(2000)
            .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
            .setRequiresCharging(true)
            .setPeriodic(2000)
            .build();



    mJobScheduler.schedule(job);

}
}

MyService类

public class MyService extends JobService {
@Override
public boolean onStartJob(JobParameters params) {
    Toast.makeText(getApplicationContext(),"onStartjob",Toast.LENGTH_SHORT).show();
    Log.i("tag","onstartjob");
    return false;
}

@Override
public boolean onStopJob(JobParameters params) {
    return false;
}
}

我的清单文件







    
        
            

            
        
    
    

    



1> koral..:

你不能同时使用setPeriodic()setOverrideDeadline().

因为在定期作业上设置此属性没有意义,所以这样做会在调用build()时抛出IllegalArgumentException.

请参阅:JobInfo.Builder javadoc

推荐阅读
虎仔球妈_459
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有