2017-05-25 128 views
0

我发现了this库,它似乎非常强大,但我无法弄清楚如何使用它。 我需要做一个当互联网连接时开始的工作,即使我的应用程序不在前台。我的工作向Web服务器发送请求以获得一些数据,并且我希望我的工作将这些数据本地存储在XML文件中。 我已经检查了this关于Android作业库的教程,但是它使用MainActivityonCreate方法安排作业,所以我的应用程序需要处于前台。如何使用Android作业执行后台作业

那么,是不是可以当Internet连接可用在Android N的后台执行作业,或者是强制性的,以有我在前台应用程序由于新的后台服务的限制?

编辑

这是我的logcat:

05-25 16:14:06.300 892-904/system_process I/ActivityManager: Force stopping clyky.cartracker appid=10117 user=-1: set debug app 
05-25 16:14:06.392 13046-13046/clyky.cartracker W/ActivityThread: Application clyky.cartracker is waiting for the debugger on port 8100... 
05-25 16:14:06.542 13046-13052/clyky.cartracker I/art: Debugger is active 
05-25 16:14:06.592 13046-13046/clyky.cartracker I/System.out: Debugger has connected 
05-25 16:14:06.592 13046-13046/clyky.cartracker I/System.out: waiting for debugger to settle... 
05-25 16:14:06.792 13046-13046/clyky.cartracker I/System.out: waiting for debugger to settle... 
05-25 16:14:06.993 13046-13046/clyky.cartracker I/System.out: waiting for debugger to settle... 
05-25 16:14:07.193 13046-13046/clyky.cartracker I/System.out: waiting for debugger to settle... 
05-25 16:14:07.393 13046-13046/clyky.cartracker I/System.out: waiting for debugger to settle... 
05-25 16:14:07.593 13046-13046/clyky.cartracker I/System.out: waiting for debugger to settle... 
05-25 16:14:07.793 13046-13046/clyky.cartracker I/System.out: waiting for debugger to settle... 
05-25 16:14:07.994 13046-13046/clyky.cartracker I/System.out: waiting for debugger to settle... 
05-25 16:14:08.194 13046-13046/clyky.cartracker I/System.out: debugger has settled (1483) 
05-25 16:14:08.289 13046-13046/clyky.cartracker W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:08.731 13046-13046/clyky.cartracker W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:08.777 13046-13046/clyky.cartracker W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:08.821 13046-13046/clyky.cartracker W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:08.870 13046-13046/clyky.cartracker W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:08.911 13046-13046/clyky.cartracker W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:08.954 13046-13046/clyky.cartracker W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:08.999 13046-13046/clyky.cartracker W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:09.041 13046-13046/clyky.cartracker W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:09.087 13046-13046/clyky.cartracker W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:09.136 13046-13046/clyky.cartracker W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:16.632 13260-13260/clyky.cartracker W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:17.031 13260-13260/clyky.cartracker W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:17.103 13260-13260/clyky.cartracker W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:17.148 13260-13260/clyky.cartracker W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:17.221 13260-13260/clyky.cartracker W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:17.282 13260-13260/clyky.cartracker W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:17.338 13325-13331/? I/art: Debugger is no longer active 
05-25 16:14:17.461 13325-13325/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:17.830 13325-13325/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:17.904 13325-13325/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:17.954 13325-13325/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:18.027 13325-13325/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:18.082 13325-13325/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:18.125 13325-13325/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:18.173 13325-13325/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:18.235 13325-13325/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:18.310 13325-13325/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 
05-25 16:14:18.408 13325-13325/clyky.cartracker W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/clyky.cartracker-2/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/arm/[email protected]@[email protected][email protected]) because non-0 exit status 

回答

0

我不知道你提到的库,但你可以在登记您的清单广播接收器,将获得触发“CONNECTIVITY_ACTION在这里根据连接状态“广播”您开始意图服务,这将执行您的后台工作。

请注意,此解决方案在Nougat之前可以正常工作,但之后您需要使用Job Scheduler。按照documentation的API“时指定的条件,如连接到未计量网络提供了一个强有力的机制来调度网络运营,都得到满足。你甚至可以使用的jobscheduler对修改内容提供商作出反应”。

0

您应该创建自己的工作与服务,要打电话,例如MyJob与为MyService:

public class MyJob extends Job { 

    public static final String TAG = "my_job_tag"; 

    @Override 
    @NonNull 
    protected Result onRunJob(Params params) { 
     Intent i = new Intent(getContext(), MyService.class); 
     getContext().startService(i); 
     return Result.SUCCESS; 
    } 

    public static void scheduleJob() { 
     new JobRequest.Builder(MyJob.TAG) 
       .setPeriodic(60_000L) // 1 minute 
       .setRequiredNetworkType(JobRequest.NetworkType.ANY) 
       .setPersisted(true) 
       .setUpdateCurrent(true) 
       .setRequirementsEnforced(true) 
       .build() 
       .schedule(); 
    } 
} 

还需要MyJobCreator

public class MyJobCreator implements JobCreator { 

    @Override 
    public Job create(String tag) { 
     if (MyJob.TAG.equals(tag)) { 
      return new MyJob(); 
     } 

     return null; 
    } 
} 

Application.onCreate()初始化你的工作:

JobManager.create(this).addJobCreator(new MyJobCreator()); 
MyJob.scheduleJob(); 
+0

我试图用一个新的服务解决方案,但我的工作是从来没有计划(在'onRunJob'它从来没有达到) – Clyky

+0

@Clyky我更新了MyJob – shmakova

+0

我尝试删除'.setPeriodic(60_000L)',因为它导致我的应用程序崩溃,但现在它在到达'build'方法时崩溃 – Clyky

0

是的与evernote/android-job你可以做的,当互联网可等人的工作,如果它不在这里后会被重复的工作是文档:

/** * 将一种网络的一些描述键入您的工作需要具有。 *不调用此函数意味着网络是没有必要的,因为默认是 * {@link网络类型#ANY}。 * *
*
* *请注意,如果最后期限是满足的要求和不执行,那么你的工作 *将运行并忽略这个要求。 * * @param networkType所需的网络类型。 * @see #setRequirementsEnforced(布尔值) * @see #setExecutionWindow(长,长) */ 公共生成器setRequiredNetworkType(@Nullable网络类型NETWORKTYPE){ mNetworkType = NETWORKTYPE; 返回此; }

,并使用它只是这样做:

int jobId = new JobRequest.Builder(YOUR_JOB_NAME.TAG) 
      .setPeriodic(TimeUnit.MINUTES.toMillis(15), TimeUnit.MINUTES.toMillis(5)) 
      .setPersisted(true) 
      .setRequiredNetworkType(JobRequest.NetworkType.CONNECTED) 
      .setExtras(extras) 
      .build() 
      .schedule(); 

我用的jobId停在即将成为这项工作。 如果你需要我的完整的例子还有就是我的github在这里我用它: https://github.com/jdagnogo/Alerte-leboncoin-appart/blob/master/app/src/main/java/com/example/jdagnogo/alertlebonsoinappart/services/jobs/GetLastAppartJob.java