ç§ãã¡ãåãçµãã§ããiFunnyã¢ããªã±ãŒã·ã§ã³ã¯ã5幎以äžåºé ã§å©çšã§ããŸãã ãã®éãã¢ãã€ã«ããŒã ã¯ããŒã«éã§ããŸããŸãªã¢ãããŒããšç§»è¡ãè¡ãå¿ èŠãããã1幎åã«èªäœã®ãœãªã¥ãŒã·ã§ã³ããåãæ¿ããŠãããããã¡ãã·ã§ããã«ãã§åºç¯ãªãã®ã®æ¹åæ§ãæ€èšããæéããããŸããã ãã®èšäºã¯ãç 究ãããŠããããšã解決çãæ€èšãããŠããããšããããŠããããäœã«ãªã£ãã®ãã«ã€ããŠãå°ãçµã£ããã®ã§ãã
ãªãããããã¹ãŠå¿ èŠãªã®ã§ããïŒ
ãã®èšäºãäœã§ãããããããŠãã®ãããã¯ãAndroidéçºããŒã ã«ãšã£ãŠéèŠã§ããããšãå€æããçç±ã称ããŠãããã«æ±ºå®ããŸãããã
- ã¢ã¯ãã£ããªãŠãŒã¶ãŒã€ã³ã¿ãŒãã§ã€ã¹ã®ãã¬ãŒã ã¯ãŒã¯å€ã§ã¿ã¹ã¯ãå®è¡ããå¿ èŠãããå Žåãå€ãã®ã·ããªãªããããŸãã
- ã·ã¹ãã ã¯ããã®ãããªã¿ã¹ã¯ã®èµ·åã«å€æ°ã®å¶éã課ããŸãã
- åããŒã«ã«ã¯é·æãšçæããããããæ¢åã®ãœãªã¥ãŒã·ã§ã³ããéžæããããšã¯éåžžã«å°é£ã§ããããšãå€æããŸããã
ã€ãã³ãã®éçºã®å¹Žè¡š
Android 0
AlarmManagerããã³ãã©ããµãŒãã¹
æåã¯ããµãŒãã¹ã«åºã¥ããŠããã¯ã°ã©ãŠã³ãããŒã¹ã®ã¿ã¹ã¯ãèµ·åããããã®ãœãªã¥ãŒã·ã§ã³ãå®è£ ãããŠããŸããã ãŸããã¿ã¹ã¯ãã©ã€ããµã€ã¯ã«ã«ãªã³ã¯ãããããããã£ã³ã»ã«ããã³åŸ©å ã§ããã¡ã«ããºã ããããŸããã ãã©ãããã©ãŒã ã¯ãã®ãããªã¿ã¹ã¯ã«å¶éã課ããŠããªããããããã¯é·ãéããŒã ã«é©ããŠããŸããã
Googleã¯ã次ã®å³ã«åºã¥ããŠãããè¡ãããšãæšå¥šããŸããã
2018幎æ«ã«ã¯ããããç解ããæå³ã¯ãããŸãããçœå®³ã®èŠæš¡ãè©äŸ¡ããã ãã§ååã§ãã
å®éãããã¯ã°ã©ãŠã³ãã§ã©ãã ãã®äœæ¥ãè¡ãããŠããã®ãã誰ãæ°ã«ããŸããã§ããã ã¢ããªã±ãŒã·ã§ã³ã¯ãå¿ èŠãªãšãã«å¿ èŠãªããšãè¡ããŸããã
é·æ ïŒ
ã©ãã§ãå©çšå¯èœã
ãã¹ãŠã«ã¢ã¯ã»ã¹ã§ããŸãã
çæ ïŒ
ã·ã¹ãã ã¯ããããæ¹æ³ã§äœæ¥ãå¶éããŸãã
æ¡ä»¶ã«ããèµ·åãªã;
APIã¯æå°éã§ãããå€ãã®ã³ãŒããèšè¿°ããå¿ èŠããããŸãã
Android 5.ããªããã
Jobcheduler
2015幎ã«è¿ã5ïŒïŒïŒå¹ŽåŸãGoogleã¯ã¿ã¹ã¯ãéå¹ççã«èµ·åãããããšã«æ°ä»ããŸããã ãŠãŒã¶ãŒã¯ãããŒãã«ããã±ããã«æšªããã£ãŠããã ãã§ãæºåž¯é»è©±ã®æ®éãå°ãªããªã£ãŠããããšãå®æçã«èšŽãå§ããŸããã
Android 5ã®ãªãªãŒã¹ã§ãJobSchedulerã®ãããªããŒã«ãç»å ŽããŸããã ããã¯ãããŸããŸãªäœæ¥ãããã¯ã°ã©ãŠã³ãã§å®è¡ã§ããã¡ã«ããºã ã§ãããããã®ã¿ã¹ã¯ã¯ããããã®ã¿ã¹ã¯ã®éäžèµ·åã·ã¹ãã ãšãã®èµ·åã®æ¡ä»¶ãèšå®ããæ©èœã«ãããæé©åããã³ç°¡çŽ åãããŸããã
ã³ãŒãã§ã¯ãããã¯éåžžã«åçŽã«èŠããŸããéå§ã€ãã³ããšçµäºã€ãã³ããçºçãããµãŒãã¹ãã¢ããŠã³ã¹ãããŸãã
ãã¥ã¢ã³ã¹ããïŒéåæã§äœæ¥ãå®è¡ããå Žåã¯ãonStartJobããã¹ããªãŒã ãéå§ããå¿ èŠããããŸãã äž»ãªããšã¯ãäœæ¥ã®æåŸã«jobFinishedã¡ãœãããåŒã³åºãããšãå¿ããªãããšã§ããããããªããšãã·ã¹ãã ã¯WakeLockã解æŸãããã¿ã¹ã¯ã¯å®äºãããšèŠãªãããã倱ãããŸãã
public class JobSchedulerService extends JobService { @Override public boolean onStartJob(JobParameters params) { doWork(params); return false; } @Override public boolean onStopJob(JobParameters params) { return false; } }
ã¢ããªã±ãŒã·ã§ã³ã®ã©ãããã§ãããã®äœæ¥ãéå§ã§ããŸãã ã¿ã¹ã¯ã¯ããã»ã¹ã§å®è¡ãããŸãããIPCã¬ãã«ã§éå§ãããŸãã å®è¡ãå¶åŸ¡ããããã«å¿ èŠãªã¿ã€ãã³ã°ã§ã®ã¿ã¢ããªã±ãŒã·ã§ã³ãèµ·åããéäžåãããã¡ã«ããºã ããããŸãã ãŸããããŸããŸãªããªã¬ãŒæ¡ä»¶ãèšå®ãããã³ãã«ãä»ããŠããŒã¿ã転éã§ããŸãã
JobInfo task = new JobInfo.Builder(JOB_ID, serviceName) .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED) .setRequiresDeviceIdle(true) .setRequiresCharging(true) .build(); JobScheduler scheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE); scheduler.schedule(task);
äžè¬ã«ãäœããªãã®ãšæ¯èŒããŠãããã¯ãã§ã«äœãã§ããã ãã ãããã®ã¡ã«ããºã ã¯API 21ã§ã®ã¿äœ¿çšã§ããŸããAndroid5.0ã®ãªãªãŒã¹æã«ããã¹ãŠã®å€ãããã€ã¹ã®ãµããŒããåæ¢ããã®ã¯å¥åŠã§ãïŒ3幎ãçµéãã4ããµããŒãããŠããŸãïŒã
é·æ ïŒ
APIã¯ã·ã³ãã«ã§ãã
å®è¡ããæ¡ä»¶ã
çæ ïŒ
å®éãAPI 23ã®ã¿ã§ã
ééããç¯ããããã
Android 5.ããªããã
GCMãããã¯ãŒã¯ãããŒãžã£ãŒ
JobSchedulerã®é¡äŒŒç-GCM Network Managerãçºè¡šãããŸããã ããã¯ãåæ§ã®æ©èœãæäŸããã©ã€ãã©ãªã§ãããAPI 9ã§æ¢ã«æ©èœããŠããŸããã確ãã«ãèŠè¿ãã«Google PlayãµãŒãã¹ãå¿ èŠã§ããã ã©ããããJobSchedulerãæ©èœããããã«å¿ èŠãªæ©èœã¯ãAndroidããŒãžã§ã³ã ãã§ãªãGPSã¬ãã«ã§ãæäŸãããããã«ãªããŸããã ãã¬ãŒã ã¯ãŒã¯ã®éçºè ã¯éåžžã«è¿ éã«èããå€ããå°æ¥ãGPSã«æ¥ç¶ããªãããšã«æ±ºããããšã«æ³šæããå¿ èŠããããŸãã 圌ãã«æè¬ããŸãã
ãã¹ãŠããŸã£ããåãã«èŠããŸãã åããµãŒãã¹ïŒ
public class GcmNetworkManagerService extends GcmTaskService { @Override public int onRunTask(TaskParams taskParams) { doWork(taskParams); return 0; } }
åãã¿ã¹ã¯ã®èµ·åïŒ
OneoffTask task = new OneoffTask.Builder() .setService(GcmNetworkManagerService.class) .setTag(TAG) .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED) .setRequiresCharging(true) .build(); GcmNetworkManager mGcmNetworkManager = GcmNetworkManager.getInstance(this); mGcmNetworkManager.schedule(task);
ã¢ãŒããã¯ãã£ã®ãã®é¡äŒŒæ§ã¯ãç¶æ¿ãããæ©èœãšãããŒã«éã®åçŽãªç§»è¡ãååŸããããšããèŠæã«ãã£ãŠæ±ºãŸããŸããã
é·æ ïŒ
JobSchedulerã«äŒŒãAPIã
API 9以éã§äœ¿çšå¯èœã§ãã
çæ ïŒ
Google Playéçºè ãµãŒãã¹ãå¿ èŠã§ã
ééããç¯ããããã
Android 5.ããªããã
WakefulBroadcastReceiver
次ã«ãJobSchedulerã§äœ¿çšãããéçºè ãçŽæ¥å©çšã§ããåºæ¬çãªã¡ã«ããºã ã®1ã€ã«ã€ããŠããã€ã説æããŸãã ããã¯WakeLockãšãã®ããŒã¹ã®WakefulBroadcastReceiverã§ãã
WakeLockã䜿çšãããšãã·ã¹ãã ããµã¹ãã³ãç¶æ ã®ãŸãŸã«ãªãã®ãé²ãããšãã§ããŸããã€ãŸããããã€ã¹ãã¢ã¯ãã£ããªç¶æ ã«ä¿ã€ããšãã§ããŸãã ããã¯ãéèŠãªäœæ¥ãè¡ãå Žåã«å¿ èŠã§ãã
WakeLockãäœæãããšãããã®èšå®ãæå®ã§ããŸããCPUãç»é¢ããŸãã¯ããŒããŒããä¿æããŸãã
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE) PowerManager.WakeLock wl = pm.newWakeLock(PARTIAL_WAKE_LOCK, "name") wl.acquire(timeout);
ãã®ã¡ã«ããºã ã«åºã¥ããŠãWakefulBroadcastReceiverãæ©èœããŸãã ãµãŒãã¹ãéå§ããWakeLockãä¿æããŸãã
public class SimpleWakefulReceiver extends WakefulBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent service = new Intent(context, SimpleWakefulService.class); startWakefulService(context, service); } }
ãµãŒãã¹ãå¿ èŠãªäœæ¥ãå®äºããåŸãåæ§ã®æ¹æ³ã§ãªãªãŒã¹ããŸãã
4ã€ã®ããŒãžã§ã³ã«ããããã®BroadcastReceiverã¯å»æ¢ãããdeveloper.android.comã§æ¬¡ã®ä»£æ¿æ段ã説æãããŸãã
- JobScheduler;
- åæã¢ããã¿
- ããŠã³ããŒããããŒãžã£ãŒ
- ãŠã£ã³ããŠã®FLAG_KEEP_SCREEN_ONã
Android 6.ãã·ã¥ãã
DozeModeïŒå€åºå ã§ã®ã¹ãªãŒã
ãã®åŸãGoogleã¯ããã€ã¹ã§å®è¡ãããã¢ããªã±ãŒã·ã§ã³ã«ããŸããŸãªæé©åãé©çšãå§ããŸããã ãããããŠãŒã¶ãŒã«ãšã£ãŠã®æé©åãšã¯ãéçºè ã«ãšã£ãŠã®å¶éã§ãã
æåã®ã¹ãããã¯DozeModeã§ãäžå®æéã¢ã€ãã«ç¶æ ã«ãªããšããã€ã¹ãã¹ãªãŒãã¢ãŒãã«ããŸãã æåã®ããŒãžã§ã³ã§ã¯ãããã¯1æéç¶ããŸãããããã®åŸã®ããŒãžã§ã³ã§ã¯ãã¹ãªãŒãæéã30åã«ççž®ãããŸããã å®æçã«ãé»è©±æ©ã¯èµ·åãããã¹ãŠã®ä¿çäžã®ã¿ã¹ã¯ãå®è¡ããåã³ã¹ãªãŒãç¶æ ã«ãªããŸãã DozeModeãŠã£ã³ããŠã¯ææ°é¢æ°çã«æ¡å€§ããŸãã ã¢ãŒãéã®ãã¹ãŠã®é·ç§»ã¯ãadbãä»ããŠè¿œè·¡ã§ããŸãã
DozeModeãçºçãããšãã¢ããªã±ãŒã·ã§ã³ã«æ¬¡ã®å¶éã課ãããŸãã
- ã·ã¹ãã ã¯ãã¹ãŠã®WakeLockãç¡èŠããŸãã
- AlarmManagerã¯é 延ããŠããŸãã
- JobSchedulerã¯æ©èœããŸããã
- SyncAdapterã¯æ©èœããŸããã
- ãããã¯ãŒã¯ã¢ã¯ã»ã¹ã¯å¶éãããŠããŸãã
DozeModeã®å¶éã«è©²åœããªãããã«ã¢ããªã±ãŒã·ã§ã³ããã¯ã€ããªã¹ãã«è¿œå ããããšãã§ããŸãããå°ãªããšãSamsungã¯ãã®ãªã¹ããå®å šã«ç¡èŠããŸããã
Android 6.ãã·ã¥ãã
AppStandbyïŒéã¢ã¯ãã£ããªã¢ããªã±ãŒã·ã§ã³
ã·ã¹ãã ã¯ãéã¢ã¯ãã£ããªã¢ããªã±ãŒã·ã§ã³ãèå¥ããDozeModeãšåãå¶éããã¹ãŠãããã«èª²ããŸãã
次ã®å Žåãã¢ããªã±ãŒã·ã§ã³ã¯åé¢ã«éä¿¡ãããŸãã
- ãã©ã¢ã°ã©ãŠã³ãã«ããã»ã¹ããããŸããã
- ã¢ã¯ãã£ããªéç¥ããããŸããã
- é€å€ãªã¹ãã«è¿œå ãããŸããã
Android 7.ãã¬ãŒ
ããã¯ã°ã©ãŠã³ãã®æé©åã ã¹ãã«ã
Svelteã¯ãGoogleãã¢ããªã±ãŒã·ã§ã³ãšã·ã¹ãã èªäœã«ããRAMæ¶è²»ãæé©åããããšããŠãããããžã§ã¯ãã§ãã
Android 7ã§ã¯ããã®ãããžã§ã¯ãã®ãã¬ãŒã ã¯ãŒã¯å ã§ãæé»ã®ãããŒããã£ã¹ãã¯éåžžã«å¹æçã§ã¯ãªãããšã決å®ãããŸããããªããªãããããã¯èšå€§ãªæ°ã®ã¢ããªã±ãŒã·ã§ã³ã§ãªãã¹ã³ããããããã®ã€ãã³ããçºçãããšã·ã¹ãã ã倧éã®ãªãœãŒã¹ãæ¶è²»ããããã§ãã ãããã£ãŠã次ã®ã¿ã€ãã®ã€ãã³ãã¯ããããã§ã¹ãã§ã®å®£èšãçŠæ¢ãããŠããŸããã
- CONNECTIVITY_ACTION;
- ACTION_NEW_PICTURE;
- ACTION_NEW_VIDEOã
Android 7.ãã¬ãŒ
FirebaseJobDispatcher
åæã«ãã¿ã¹ã¯èµ·åãã¬ãŒã ã¯ãŒã¯ã®æ°ããããŒãžã§ã³ã§ããFirebaseJobDispatcherãå ¬éãããŸããã å®éãããã¯å®æããGCM NetworkManagerã§ãããå°ãæŽçãããå°ãæè»ã«ãªããŸããã
èŠèŠçã«ã¯ããã¹ãŠããŸã£ããåãã«èŠããŸããã åããµãŒãã¹ïŒ
public class JobSchedulerService extends JobService { @Override public boolean onStartJob(JobParameters params) { doWork(params); return false; } @Override public boolean onStopJob(JobParameters params) { return false; } }
圌ãšã®å¯äžã®éãã¯ããã©ã€ããŒãã€ã³ã¹ããŒã«ã§ããããšã§ãã ãã©ã€ããŒã¯ãã¿ã¹ã¯ã®èµ·åæŠç¥ãæ åœããã¯ã©ã¹ã§ãã
ã¿ã¹ã¯ã®èµ·åèªäœã¯æéã®çµéãšãšãã«å€åããŠããŸããã
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context)); Job task = dispatcher.newJobBuilder() .setService(FirebaseJobDispatcherService.class) .setTag(TAG) .setConstraints(Constraint.ON_UNMETERED_NETWORK, Constraint.DEVICE_IDLE) .build(); dispatcher.mustSchedule(task);
é·æ ïŒ
JobSchedulerã«äŒŒãAPIã
API 9以éã§äœ¿çšå¯èœã§ãã
çæ ïŒ
Google Playéçºè ãµãŒãã¹ãå¿ èŠã§ã
ééããç¯ããããã
GPSãåãé€ãããã«ãã©ã€ããŒãã€ã³ã¹ããŒã«ããããšã¯åæ°ã¥ããããŸããã æ€çŽ¢ãããŸããããæçµçã«ã¯æ¬¡ã®ããšãããããŸããã
Googleã¯ãããç¥ã£ãŠããŸããããããã®ã¿ã¹ã¯ã¯æ°å¹ŽééããŠããŸãã
Android 7.ãã¬ãŒ
Evernoteã«ããAndroidãžã§ã
ãã®çµæãã³ãã¥ããã£ã¯ããã«èãããããEvernoteã®ã©ã€ãã©ãªã®åœ¢ã§èªäœã®ãœãªã¥ãŒã·ã§ã³ãç»å ŽããŸããã ããã ãã§ã¯ãããŸããã§ããããEvernoteã®ãœãªã¥ãŒã·ã§ã³ã§ãããããèªäœã確ç«ããã人ã ã«å€¢äžã«ãªã£ããããšãã§ããŸããã
ã¢ãŒããã¯ãã£é¢ã§ã¯ããã®ã©ã€ãã©ãªã¯åã®ã©ã€ãã©ãªããã䟿å©ã§ããã
ã¿ã¹ã¯ã®äœæãæ åœãããšã³ãã£ãã£ãç»å ŽããŸããã JobSchedulerã®å Žåããããã¯ãªãã¬ã¯ã·ã§ã³ã«ãã£ãŠäœæãããŸããã
class SendLogsJobCreator : JobCreator { override fun create(tag: String): Job? { when (tag) { SendLogsJob.TAG -> return SendLogsJob() } return null } }
ã¿ã¹ã¯èªäœã§ããå¥ã®ã¯ã©ã¹ããããŸãã JobSchedulerã§ã¯ãããã¯ãã¹ãŠonStartJobå ã®ã¹ã€ããã«ãã³ããããŸããã
class SendLogsJob : Job() { override fun onRunJob(params: Params): Result { return doWork(params) } }
ã¿ã¹ã¯ã®èµ·åã¯åãã§ãããEvernoteã¯ç¶æ¿ãããã€ãã³ãã«å ããŠãæ¯æ¥ã®ã¿ã¹ã¯ãç¬èªã®ã¿ã¹ã¯ããŠã£ã³ããŠå ã§ã®èµ·åãªã©ãç¬èªã®ã¿ã¹ã¯ãè¿œå ããŸããã
new JobRequest.Builder(JOB_ID) .setRequiresDeviceIdle(true) .setRequiresCharging(true) .setRequiredNetworkType(JobRequest.NetworkType.UNMETERED) .build() .scheduleAsync();
é·æ ïŒ
䟿å©ãªAPIã
ãã¹ãŠã®ããŒãžã§ã³ã§ãµããŒããããŠããŸãã
Google PlayãµãŒãã¹ã¯å¿ èŠãããŸããã
çæ ïŒ
ãµãŒãããŒãã£ã®ãœãªã¥ãŒã·ã§ã³ã
圌ãã¯ã©ã€ãã©ãªãç©æ¥µçã«ãµããŒãããŸããã é倧ãªåé¡ãããªããããŸãããããã¹ãŠã®ããŒãžã§ã³ãšãã¹ãŠã®ããã€ã¹ã§æ©èœããŸããã ãã®çµæãæšå¹ŽGoogleã®ã©ã€ãã©ãªããµããŒãã§ããªãããã€ã¹ã®å€§ããªã¬ã€ã€ãŒãã«ãããããããAndroidããŒã ã¯æšå¹ŽEvernoteã®ãœãªã¥ãŒã·ã§ã³ãéžæããŸããã
å éšã§ã¯ã極端ãªå Žåã«AlarmManagerã䜿çšããŠGoogleã®ãœãªã¥ãŒã·ã§ã³ã«åãçµã¿ãŸããã
Android 8.ãªã¬ãª
ããã¯ã°ã©ãŠã³ãå®è¡ã®å¶é
å¶éã«æ»ããŸãããã æ°ããAndroidã®ç»å Žã«ãããæ°ããæé©åãè¡ãããŸããã Googleã®ã¹ã¿ããã¯å¥ã®åé¡ãçºèŠããŸããã ä»åã¯ãã¹ãŠãµãŒãã¹ãšãããŒããã£ã¹ãã«ã€ããŠã§ããïŒã¯ããæ°ãããã®ã¯ãããŸããïŒã
-
ããã¯ã°ã©ãŠã³ãã®ã¢ããªã±ãŒã·ã§ã³ã®å Žåã¯startService -
ãããã§ã¹ãã§ã®æé»çãªãããŒããã£ã¹ã
ãŸããããã¯ã°ã©ãŠã³ããããµãŒãã¹ãéå§ããããšã¯çŠæ¢ãããŠããŸããã ãæ³ã®æ çµã¿ãã§ã¯ããã©ã¢ã°ã©ãŠã³ããµãŒãã¹ã®ã¿ã§ããã çŸåšããµãŒãã¹ã¯å»æ¢ãããŠãããšèšããŸãã
2çªç®ã®å¶éã¯åããããŒããã£ã¹ãã§ãã ä»åã¯ããããã§ã¹ããžã®ãã¹ãŠã®æé»çãªãããŒããã£ã¹ãã®ç»é²ãçŠæ¢ãããŸããã æé»çãããŒããã£ã¹ãã¯ãç§ãã¡ã®ã¢ããªã±ãŒã·ã§ã³ã ãã§ãªãããããŒããã£ã¹ãã§ãã ããšãã°ãã¢ã¯ã·ã§ã³ACTION_PACKAGE_REPLACEDããããACTION_MY_PACKAGE_REPLACEDããããŸãã ãããã£ãŠãæåã®ãã®ã¯æé»çã§ãã
ãã ãããããŒããã£ã¹ãã¯Context.registerBroadcastãä»ããŠç»é²ã§ããŸãã
Android 9.ãã€
ã¯ãŒã¯ãããŒãžã£ãŒ
ãã®æé©åã¯ãŸã åæ¢ããŠããŸãã ãããããããã€ã¹ã¯ãšãã«ã®ãŒæ¶è²»ã®èŠ³ç¹ããè¿ éãã€æ éã«åäœãå§ããã®ã§ãããã ããããããŠãŒã¶ãŒã¯ããã«ã€ããŠããŸãèŠæ ãèšããªãã§ãããã
Android 9ã§ã¯ããã¬ãŒã ã¯ãŒã¯ã®éçºè ãã¿ã¹ã¯ãèµ·åããããŒã«ã«åŸ¹åºçã«ã¢ãããŒãããŸããã ãã¹ãŠã®å·®ãè¿«ã£ãåé¡ã解決ããããã«ãWorkManagerã®ããã¯ã°ã©ãŠã³ãã¿ã¹ã¯ãèµ·åããããã®ã©ã€ãã©ãªãGoogle I / Oã§èµ·åãããŸããã
Googleã¯æè¿ãAndroidã¢ããªã±ãŒã·ã§ã³ã®ã¢ãŒããã¯ãã£ã«é¢ããããžã§ã³ã圢æããããšããŠãããéçºè ã«ããã«å¿ èŠãªããŒã«ãæäŸããŠããŸãã ãã®ãããLiveDataãViewModelãããã³Roomãå«ãã¢ãŒããã¯ãã£ã³ã³ããŒãã³ãããããŸããã WorkManagerã¯ãã¢ãããŒããšãã©ãã€ã ãåççã«è£å®ããããã«èŠããŸãã
WorkManagerãå éšã«ã©ã®ããã«é 眮ãããŠãããã説æããå Žåãæè¡çãªãã¬ãŒã¯ã¹ã«ãŒã¯ãããŸããã æ¬è³ªçã«ãããã¯æ¢åã®ãœãªã¥ãŒã·ã§ã³ã®ã©ãããŒã§ãïŒJobSchedulerãFirebaseJobDispatcherãAlarmManagerã
createBestAvailableBackgroundScheduler
static Scheduler createBestAvailableBackgroundScheduler(Context, WorkManager) { if (Build.VERSION.SDK_INT >= MIN_JOB_SCHEDULER_API_LEVEL) { return new SystemJobScheduler(context, workManager); } try { return tryCreateFirebaseJobScheduler(context); } catch (Exception e) { return new SystemAlarmScheduler(context); } }
éžæã³ãŒãã¯éåžžã«ç°¡åã§ãã ãã ããJobSchedulerã¯API 21以éã§äœ¿çšã§ããŸãããæåã®ããŒãžã§ã³ã¯ããªãäžå®å®ã ã£ããããAPI 23ã§ã®ã¿äœ¿çšããããšã«æ³šæããŠãã ããã
ããŒãžã§ã³ã23æªæºã®å Žåããªãã¬ã¯ã·ã§ã³ãéããŠFirebaseJobDispatcherãèŠã€ããããšããŸããããã§ãªãå Žåã¯ãAlarmManagerã䜿çšããŸãã
ã©ãããŒãéåžžã«æè»ã«ãªã£ãããšã¯æ³šç®ã«å€ããŸãã ä»åãéçºè ã¯ãã¹ãŠãåå¥ã®ãšã³ãã£ãã£ã«åå²ããã¢ãŒããã¯ãã£çã«ã¯äŸ¿å©ã«èŠããŸãïŒ
- ã¯ãŒã«ãŒ-äœæ¥ããžãã¯ã
- WorkRequest-ã¿ã¹ã¯èµ·åã®ããžãã¯ã
- WorkRequest.Builder-ãã©ã¡ãŒã¿ãŒã
- å¶çŽ-æ¡ä»¶;
- WorkManager-ã¿ã¹ã¯ã管çãããããŒãžã£ãŒã
- WorkStatus-ã¿ã¹ã¯ã®ã¹ããŒã¿ã¹ã
èµ·åæ¡ä»¶ã¯JobSchedulerããç¶æ¿ãããŸããã
URIãå€æŽããããªã¬ãŒã¯API 23ã§ã®ã¿åºçŸããããšã«æ³šæããŠãã ãããããã«ãã¡ãœããã®ãã©ã°ã䜿çšããŠãç¹å®ã®URIã ãã§ãªãããã¹ãŠã®ãã¹ããããURIã®å€æŽã«ãµãã¹ã¯ã©ã€ãã§ããŸãã
ç§ãã¡ã«ã€ããŠè©±ããšãã¢ã«ãã¡æ®µéã§WorkManagerã«åãæ¿ããããšã決å®ãããŸããã
ããã«ã¯ããã€ãã®çç±ããããŸãã Evernoteã«ã¯ãã©ã€ãã©ãªã®éçºè ãWorkManagerãçµ±åãããããŒãžã§ã³ãžã®ç§»è¡ã§ä¿®æ£ããããšãçŽæããããã€ãã®é倧ãªãã°ããããŸãã ãŸããGoogleã®æ±ºå®ãEvernoteã®å©ç¹ãç¡å¹ã«ããããšã«åœŒããåæããŠããŸãã ããã«ããã®ãœãªã¥ãŒã·ã§ã³ã¯ãã¢ãŒããã¯ãã£ã³ã³ããŒãã³ãã䜿çšããŠãããããã¢ãŒããã¯ãã£ã«é©ããŠããŸãã
次ã«ããã®ã¢ãããŒããã©ã®ããã«äœ¿çšããããç°¡åãªäŸã§ç€ºããããšæããŸãã åæã«ãWorkManagerãæã£ãŠãããJobSchedulerãæã£ãŠãããã¯ããã»ã©éèŠã§ã¯ãããŸããã
éåžžã«åçŽãªã±ãŒã¹ã®äŸãèŠãŠã¿ãŸãããããåå ¬éããªã©ãã¯ãªãã¯ããŸãã
çŸåšããã¹ãŠã®ã¢ããªã±ãŒã·ã§ã³ã¯ãããã¯ãŒã¯ãžã®ãããã¯èŠæ±ããéããããšããŠããŸããããã«ããããŠãŒã¶ãŒã¯ç·åŒµããŠåŸ æ©ããŸããããã®æç¹ã§ã¢ããªã±ãŒã·ã§ã³å ã§è³Œå ¥ããããåºåãèŠãããšãã§ããŸãã
ãã®ãããªå ŽåãããŒã«ã«ããŒã¿ãæåã«å€æŽãããŸã-ãŠãŒã¶ãŒã¯ããã«ã¢ã¯ã·ã§ã³ã®çµæã確èªããŸãã ãã®åŸãããã¯ã°ã©ãŠã³ãã§ãµãŒããŒãžã®èŠæ±ãããããµãŒããŒã倱æããå ŽåãããŒã¿ã¯åæç¶æ ã«ãªã»ãããããŸãã
次ã«ããããã©ã®ããã«èŠãããã®äŸã瀺ããŸãã
JobRunnerã«ã¯ãã¿ã¹ã¯ãèµ·åããããã®ããžãã¯ãå«ãŸããŠããŸãã 圌ã®ã¡ãœããã¯ãã¿ã¹ã¯ã®æ§æãèšè¿°ãããã©ã¡ãŒã¿ãŒãæž¡ããŸãã
JobRunner.java
fun likePost(content: IFunnyContent) { val constraints = Constraints.Builder() .setRequiredNetworkType(NetworkType.CONNECTED) .build() val input = Data.Builder() .putString(LikeContentJob.ID, content.id) .build() val request = OneTimeWorkRequest.Builder(LikeContentJob::class.java) .setInputData(input) .setConstraints(constraints) .build() WorkManager.getInstance().enqueue(request) }
WorkManagerå ã®ã¿ã¹ã¯èªäœã¯æ¬¡ã®ãšããã§ãããã©ã¡ãŒã¿ãŒããIDãååŸãããµãŒããŒäžã®ã¡ãœãããåŒã³åºããŠãã®ã³ã³ãã³ããè©äŸ¡ããŸãã
次ã®ããžãã¯ãå«ãåºæ¬ã¯ã©ã¹ããããŸãã
abstract class BaseJob : Worker() { final override fun doWork(): Result { val workerInjector = WorkerInjectorProvider.injector() workerInjector.inject(this) return performJob(inputData) } abstract fun performJob(params: Data): Result }
ãŸããWorkerã®æ瀺çãªç¥èããå°ãé¢ããããšãã§ããŸãã ãŸããWorkerInjectorã«ããäŸåé¢ä¿æ³šå ¥ããžãã¯ãå«ãŸããŠããŸãã
WorkerInjectorImpl.java
@Singleton public class WorkerInjectorImpl implements WorkerInjector { @Inject public WorkerInjectorImpl() {} @Ovierride public void inject(Worker job) { if (worker instanceof AppCrashedEventSendJob) { Injector.getAppComponent().inject((AppCrashedEventSendJob) job); } else if (worker instanceof CheckNativeCrashesJob) { Injector.getAppComponent().inject((CheckNativeCrashesJob) job); } } }
Daggerãžã®åŒã³åºããåã«ãããã·ããŸããããã¹ãã«ã¯åœ¹ç«ã¡ãŸããã€ã³ãžã§ã¯ã¿ãŒã®å®è£ ã眮ãæããã¿ã¹ã¯ã«å¿ èŠãªç°å¢ãå®è£ ããŸãã
fun void testRegisterPushProvider() { WorkManagerTestInitHelper.initializeTestWorkManager(context) val testDriver = WorkManagerTestInitHelper.getTestDriver() WorkerInjectorProvider.setInjector(TestInjector()) // mock dependencies val id = jobRunner.runPushRegisterJob() testDriver.setAllConstraintsMet(id) Assert.assertTrue(âŠ) }
class LikePostInteractor @Inject constructor( val iFunnyContentDao: IFunnyContentDao, val jobRunner: JobRunner) : Interactor { fun execute() { iFunnyContentDao.like(getContent().id) jobRunner.likePost(getContent()) } }
ã€ã³ã¿ã©ã¯ã¿ãŒã¯ãã¹ã¯ãªããã®ééãéå§ããããã«ViewControllerããã«ãããšã³ãã£ãã£ã§ãïŒãã®å Žåããã®ããã«ïŒã ã³ã³ãã³ããããŒã«ã«ã§ãã¢ããããŒãæžã¿ããšããŠããŒã¯ããå®è¡ã®ããã«ã¿ã¹ã¯ãéä¿¡ããŸãã ã¿ã¹ã¯ã倱æãããšãåæ§ã®ãã®ãåé€ãããŸãã
class IFunnyContentViewModel(val iFunnyContentDao: IFunnyContentDao) : ViewModel() { val likeState = MediatorLiveData<Boolean>() var iFunnyContentId = MutableLiveData<String>() private var iFunnyContentState: LiveData<IFunnyContent> = attachLiveDataToContentId(); init { likeState.addSource(iFunnyContentState) { likeState.postValue(it!!.hasLike) } } }
Googleã®ã¢ãŒããã¯ãã£ã³ã³ããŒãã³ãã§ããViewModelãšLiveDataã䜿çšããŸãã ãããViewModelã®å€èŠ³ã§ãã ããã§ãDAOã®ãªããžã§ã¯ãã®æŽæ°ãlikeã®ã¹ããŒã¿ã¹ã«é¢é£ä»ããŸãã
IFunnyContentViewController.java
class IFunnyContentViewController @Inject constructor( private val likePostInteractor: LikePostInteractor, val viewModel: IFunnyContentViewModel) : ViewController { override fun attach(view: View) { viewModel.likeState.observe(lifecycleOwner, { updateLikeView(it!!) }) } fun onLikePost() { likePostInteractor.setContent(getContent()) likePostInteractor.execute() } }
ViewControllerã¯ãäžæ¹ã§ã¯åé¡ã®ã¹ããŒã¿ã¹ã®å€æŽã«ãµãã¹ã¯ã©ã€ãããä»æ¹ã§ã¯å¿ èŠãªã¹ã¯ãªããã®ééãéå§ããŸãã
ãããŠãããã¯ç§ãã¡ãå¿ èŠãšããå®è³ªçã«ãã¹ãŠã®ã³ãŒãã§ãã DAOã®å®è£ ãšåæ§ã«ãViewèªäœã®åäœãè¿œå ããããšã¯æ®ã£ãŠããŸãã ã«ãŒã ã䜿çšããå Žåã¯ããªããžã§ã¯ãã«ãã£ãŒã«ããç»é²ããã ãã§ãã ãšãŠãã·ã³ãã«ã§å¹æçã§ãã
ãŸãšãããš
JobSchedulerãGCM Network ManagerãFirebaseJobDispatcherïŒ
- ãããã䜿çšããªãã§ãã ãã
- ãããã«ã€ããŠã®èšäºãããèªãŸãªã
- ã¬ããŒããèŠãªã
- ã©ã¡ããéžã¶ã¹ããèããªãã§ãã ããã
Evernoteã«ããAndroidãžã§ãïŒ
- å éšã§ã¯WorkManagerã䜿çšããŸãã
- ãœãªã¥ãŒã·ã§ã³éã§é倧ãªãã°ããŒãããŠããŸãã
WorkManagerïŒ
- APIã¬ãã«9+;
- Google Play Servicesããç¬ç«ããŠããŸãã
- ãã§ãŒã³/ InputMergers;
- 察åŠçã¢ãããŒã;
- Googleããã®ãµããŒãïŒä¿¡ãããïŒã