Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cookpad TechKitchen#6-android-with-ml-and-sched...

kazy1991
March 23, 2017

Cookpad TechKitchen#6-android-with-ml-and-scheduler

kazy1991

March 23, 2017
Tweet

More Decks by kazy1991

Other Decks in Technology

Transcript

  1. T

  2. +PC4DIFEVMFSαϯϓϧ 4FSWJDFଆ public class MyJobService extends JobService {
 
 @Override


    public boolean onStartJob(JobParameters job) { // ͜͜ʹॲཧΛهड़͢Δ
 return false;
 }
 
 @Override
 public boolean onStopJob(JobParameters job) {
 return false;
 }
 } <service
 android:name=".MyJobService"
 android:exported="true"
 android:permission="android.permission.BIND_JOB_SERVICE" />
  3. +PC4DIFEVMFSαϯϓϧ 4FSWJDFଆ public class MyJobService extends JobService {
 @Override
 public

    boolean onStartJob(JobParameters params) { // ͜͜͸ϝΠϯεϨου
 tooHeavyJob()
 .subscribeOn(Schedulers.io())
 .subscribe(result -> {
 // Կ͔ߋ৽ॲཧͳͲΛߦ͏
 jobFinished(params, false);
 // δϣϒ͕ࣦഊͨ͠৔߹͸trueΛ౉͢ͱ࠶εέδϡʔϧ
 });
 // ಉظॲཧͷ৔߹͸false/ඇಉظॲཧͷ৔߹͸trueΛฦ͢
 return true;
 }
 
 @Override
 public boolean onStopJob(JobParameters params) { // δϣϒ͕׬ྃ͢ΔલʹγεςϜ͕Ωϟϯηϧ͢Δͱݺ͹ΕΔ
 return false;
 }
 }
  4. +PC4DIFEVMFSαϯϓϧ εέδϡʔϧॲཧ 
 public class MyJobService extends JobService {
 


    private static final int PERIODIC_JOB_ID = 1;
 
 public static void setPeriodicSchedule(Context context) {
 JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
 
 JobInfo jobInfo = new JobInfo.Builder(PERIODIC_JOB_ID,
 new ComponentName(context, MyJobService.class))
 .setPeriodic(TimeUnit.MINUTES.toMillis(30))
 .setPersisted(true)
 .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
 .build();
 scheduler.schedule(jobInfo);
 }
 
 public static void cancelPeriodicSchedule(Context context) {
 JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
 scheduler.cancel(PERIODIC_JOB_ID);
 } }
  5. +PC4DIFEVMFSͰεέδϡʔϧʹར༻Ͱ͖Δ৚݅  ࠷େకΊ੾Γ࣌ؒ TFU0WFSSJEF%FBEMJOF   ࠷௿஗Ԇ࣌ؒ TFU.JOJNVN-BUFODZ  

    όοΫΦϑϦτϥΠํ਑ TFU#BDLPGG$SJUFSJB   ௨৴؀ڥͷ੍໿ TFU3FRVJSFE/FUXPSL5ZQF   ॆి؀ڥͷ੍໿ TFU3FRVJSFT$IBSHJOH   ΞΠυϧঢ়ଶͷ੍໿ TFU3FRVJSFT%FWJDF*EMF   ܁Γฦ͠ͷεέδϡʔϦϯά TFU1FSJPEJD   ࠶ىಈ࣌ͷӬଓԽ TFU1FSTJTUFE
  6. +PC4DIFEVMFSʹύϥϝʔλΛ౉͢  +PC*OGP#VJMEFSTFU&YUSBT 1FSTJTUBCMF#VOEMFFYUSBT 
 PersistableBundle bundle = new PersistableBundle();


    JobInfo jobInfo = new JobInfo.Builder(JOB_TAG,
 new ComponentName(this, MyJobService.class))
 .setExtras(bundle)
 .build(); public class MyJobService extends JobService {
 @Override
 public boolean onStartJob(JobParameters params) {
 PersistableBundle bundle = params.getExtras();
 // PersistableBundle͸εέδϡʔϧ࣌ʹӬଓԽ͞ΕΔͷͰɺ
 // ͜͜Ͱॻ͖׵͑ͯ΋࣍ճͷjobʹ͸൓ө͞Εͳ͍
 return false;
 }
 }  +PC1BSBNFUFSTHFU&YUSBT
  7. εέδϡʔϧঢ়گΛίʔυͰ֬ೝ͢Δ  +PC4DIFEVMFSHFU"MM1FEJOH+PCT  εέδϡʔϧ͞Ε͍ͯΔKPC*OGPϦετ͕ड͚औΕΔ  εέδϡʔϧͨ͠ࡍʹ౉ͨ͠*%Λൺֱ͢Δ 
 public static

    boolean isPending(Context context, int serviceId) {
 JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
 List<JobInfo> pendingJobList = scheduler.getAllPendingJobs();
 for (JobInfo jobInfo : pendingJobList) {
 if (serviceId == jobInfo.getId()) {
 return true;
 }
 }
 return false;
 }
  8. ύοέʔδߋ৽ΠϕϯτΛϋϯυϦϯά͢Δ <receiver android:name=".receiver.PackageReplacedBroadcastReceiver">
 <intent-filter>
 <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
 </intent-filter>
 </receiver> @Override


    public void onReceive(Context context, Intent intent) {
 super.onReceive(context, intent);
 String action = intent.getAction();
 if (Intent.ACTION_MY_PACKAGE_REPLACED.equals(action)) {
 // εέδϡʔϧ͞Ε͍ͯΔ͔֬ೝ͢Δɻ
 }
 }
  9. Ұ౓͖Γͷδϣϒ͕ճىಈ͢Δ  ݱ৅  Ұ౓͖Γͷεέδϡʔϧͷ͸͕ͣෳ਺ճىಈ͢Δ  ࠶ݱ৚݅  /ະຬͷશͯͷ୺຤ 

    +PCͷ࣮ߦ࣌ؒΑΓ%FBEMJOF͕୹͍ͱෳ਺ճىಈ͢Δ  ରԠࡦ  %FBEMJOFΛ৳͹͢ɺνΣοΫػߏΛ༻ҙ͢Δ IUUQTUBDLPWFSGMPXDPNRVFTUJPOTKPCTDIFEVMFSQPTUJOHKPCTUXJDFOPUFYQFDUFE