2017-05-25 4 views
0

J'ai découvert la bibliothèque this qui semble être très puissante, mais je n'arrive pas à comprendre comment l'utiliser. Je dois faire un travail qui commence quand Internet est connecté, même si mon application n'est pas au premier plan. Mon travail envoie une requête à un serveur Web pour obtenir des données et je souhaite que mon travail stocke ces données localement dans un fichier XML. J'ai vérifié this tutoriel sur Android-Job bibliothèque, mais il planifie le travail dans la méthode onCreateMainActivity, donc mon application devrait être au premier plan.Comment exécuter un travail d'arrière-plan avec Android-Job

Alors, est-il possible d'exécuter un travail en arrière-plan lorsqu'une connexion Internet est disponible sur Android N ou est-il obligatoire d'avoir mon application au premier plan en raison de nouvelles restrictions de services d'arrière-plan?

EDIT

Ceci est mon 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]asses.dex) 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]_lib_slice_1_a[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 

Répondre

0

Je ne suis pas sûr de cette bibliothèque que vous avez mentionné, mais vous pouvez enregistrer un récepteur de diffusion dans votre manifeste qui va se déclencher pour « CONNECTIVITY_ACTION Diffusions "ici basé sur l'état de connectivité que vous démarrez le service d'intention qui vous effectuera le travail d'arrière-plan.

Notez que cette solution fonctionnera correctement avant Nougat, mais après cela, vous devrez utiliser Job Scheduler. Selon l'documentation, l'API "fournit un mécanisme robuste pour planifier les opérations réseau lorsque des conditions spécifiées, telles que la connexion à un réseau non mesuré, sont satisfaites. Vous pouvez même utiliser JobScheduler pour réagir aux modifications apportées aux fournisseurs de contenu".

0

Vous devez créer votre propre emploi avec service, que vous voulez appeler, par exemple MyJob avec 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(); 
    } 
} 

vous devez également MyJobCreator:

public class MyJobCreator implements JobCreator { 

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

     return null; 
    } 
} 

et init votre travail en Application.onCreate() :

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

J'ai essayé votre solution avec un nouveau service, mais mon travail est jamais prévu (le 'onRunJob' il n'a jamais atteint) – Clyky

+0

@Clyky J'ai mis à jour MyJob – shmakova

+0

J'ai essayé d'enlever '.setPeriodic (60_000L)' parce qu'il a provoqué le crash de mon application, mais maintenant il plante quand il atteint la méthode 'build' – Clyky

0

Oui avec evernote/android-job vous pouvez faire un travail lorsque Internet est disponible et si ce n'est pas le travail sera répété après voici la documentation:

/** * Définir une description du genre de type réseau à vos besoins d'emploi avoir. * Le fait de ne pas appeler cette fonction signifie que le réseau n'est pas nécessaire, car la valeur par défaut est * {@link NetworkType # ANY}. * *
*
* * Notez que si le délai est respecté et les exigences ne sont pas appliquées, alors votre travail * courront et ne pas tenir compte de cette exigence. * * @param networkType Type de réseau requis. * @see #setRequirementsEnforced (Boolean) * @see #setExecutionWindow (long, long) */ setRequiredNetworkType Builder public (@Nullable NetworkType networkType) { mNetworkType = networkType; retourner ceci; }

Et pour l'utiliser juste faire ceci:

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(); 

J'ai utilisé le JOBID pour arrêter ce travail dans le futur. Si vous avez besoin de mon exemple complet, il est mon github où je l'ai utilisé: https://github.com/jdagnogo/Alerte-leboncoin-appart/blob/master/app/src/main/java/com/example/jdagnogo/alertlebonsoinappart/services/jobs/GetLastAppartJob.java