Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Droidkaigi 2017

kazy1991
March 09, 2017

Droidkaigi 2017

kazy1991

March 09, 2017
Tweet

More Decks by kazy1991

Other Decks in Technology

Transcript

  1. "MBSN.BOBHFSαϯϓϧ #SPBEDBTU3FDFJWFS 
 public class AlarmReceiver extends BroadcastReceiver {
 


    private static Intent createIntent(Context context) {
 return new Intent(context, AlarmReceiver.class);
 }
 
 public static PendingIntent createPendingIntent(Context context) {
 return PendingIntent.getBroadcast(context, 0, createIntent(context), 0);
 }
 
 @Override
 public void onReceive(Context context, Intent intent) {
 // ͜͜ͰServiceΛىಈ͢ΔͳͲ..
 }
 } # AndroidManifest.xml
 <receiver
 android:name=".AlarmReceiver"
 android:exported="false" />
  2. "MBSN.BOBHFSαϯϓϧ εέδϡʔϧ 
 public class MainActivity extends AppCompatActivity {
 @Override


    protected void onCreate(Bundle savedInstanceState) { //..
 PendingIntent intent = AlarmReceiver.createPendingIntent(this);
 AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
 alarmManager.set(AlarmManager.RTC_WAKEUP, fiveSecondsLater(), intent);
 }
 }  1FOEJOH*OUFOUͱىಈ࣌ؒΛ"MBSN.BOBHFSʹ౉͢
  3. +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" />
  4. +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;
 }
 }
  5. +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);
 } }
  6. +PC4DIFEVMFSͰεέδϡʔϧʹར༻Ͱ͖Δ৚݅  ࠷େకΊ੾Γ࣌ؒ TFU0WFSSJEF%FBEMJOF   ࠷௿஗Ԇ࣌ؒ TFU.JOJNVN-BUFODZ  

    όοΫΦϑϦτϥΠํ਑ TFU#BDLPGG$SJUFSJB   ௨৴؀ڥͷ੍໿ TFU3FRVJSFE/FUXPSL5ZQF   ॆి؀ڥͷ੍໿ TFU3FRVJSFT$IBSHJOH   ΞΠυϧঢ়ଶͷ੍໿ TFU3FRVJSFT%FWJDF*EMF   ܁Γฦ͠ͷεέδϡʔϦϯά TFU1FSJPEJD   ࠶ىಈ࣌ͷӬଓԽ TFU1FSTJTUFE
  7. +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
  8. εέδϡʔϧঢ়گΛίʔυͰ֬ೝ͢Δ  +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;
 }
  9. EVNQTZTKPCTDIFEVMFS 04ͷ৔߹ 
 Settings: <୺຤ͷઃఆ> Started users: [0]
 Registered 48

    jobs:
 JOB #1000/800 : com.android.server.pm.BackgroundDexOptService <JobInfoʹ౉ͨ͠εέδϡʔϧ৚݅> JOB #1000/20536: com.android.server.backup.FullBackupJob
 <JobInfoʹ౉ͨ͠εέδϡʔϧ৚݅>
 [த൫͸লུ]
 Job history: -5m54s389ms STOP: u0a69 com.example.BackgroundService -5m51s543ms STOP: u0a83 com.example.AuthService
 -21s184ms START: u0a15 com.example.ServiceName -20s940ms STOP: u0a15 com.example.ServiceName

  10. ύοέʔδߋ৽ΠϕϯτΛϋϯυϦϯά͢Δ <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)) {
 // εέδϡʔϧ͞Ε͍ͯΔ͔֬ೝ͢Δɻ
 }
 }
  11. ୹͗͢Δ%FBE-JOF  ݱ৅  %FBE-JOFͷ੍໿͕ҙਤ௨Γಈ͔ͳ͍  ࠶ݱ৚݅  %FBE-JOFʹͱͯ΋୹͍࣌ؒΛઃఆ͢Δ T͘Β͍

      Ұ෦ͷ୺຤ͰKPCͷىಈʹ਺෼͔͔Δ  ରࡦ  +PC4DIFEVMFSΛଈ࣌ੑΛٻΊͯ࢖ͬͯ͸͍͚ͳ͍#
  12. Ұ౓͖Γͷδϣϒ͕ճىಈ͢Δ  ݱ৅  Ұ౓͖Γͷεέδϡʔϧͷ͸͕ͣෳ਺ճىಈ͢Δ  ࠶ݱ৚݅  /ະຬͷશͯͷ୺຤ 

    +PCͷ࣮ߦ࣌ؒΑΓ%FBEMJOF͕୹͍ͱෳ਺ճىಈ͢Δ  ରԠࡦ  %FBEMJOFΛ৳͹͢ɺνΣοΫػߏΛ༻ҙ͢Δ IUUQTUBDLPWFSGMPXDPNRVFTUJPOTKPCTDIFEVMFSQPTUJOHKPCTUXJDFOPUFYQFDUFE
  13. ա৒ʹαʔϏεΛىಈ͢Δ୺຤  ݱ৅  Ұ෦ͷ୺຤Ͱ૝ఆͷ਺ेഒͷδϣϒىಈ͢Δ  ࠶ݱ৚݅  ෆ໌ 04

    ػछͳͲʹภΓ͕ͳ͍   ରԠࡦ  νΣοΫػߏ༻ҙ͢ΔPS΂͖౳ͳ࣮૷Λҙࣝ͢Δ
  14. ($./FUXPSL.BOBHFSαϯϓϧ αʔϏεଆ public class MyTaskService extends GcmTaskService {
 @Override
 public

    void onInitializeTasks() {
 // ΞϓϦ͕࡟আ͞ΕͨΓɺߋ৽͞Εͨ৔߹͸εέδϡʔϧ͕࡟আ͞ΕΔͷͰɺ
 // ͜͜ʹ࠶εέδϡʔϧ͢ΔίʔυΛॻ͘(ॳճ͸ݺ͹Εͳ͍)
 }
 
 @Override
 public int onRunTask(TaskParams taskParams) {
 // ͜͜ʹॲཧΛهड़͢Δ(ϫʔΧʔεϨου಺)
 return GcmNetworkManager.RESULT_SUCCESS;
 }
 } <service
 android:name=".MyTaskService"
 android:exported="true"
 android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE">
 <intent-filter>
 <action android:name="com.google.android.gms.gcm.ACTION_TASK_READY" />
 </intent-filter>
 </service>
  15. ($./FUXPSL.BOBHFSαϯϓϧ εέδϡʔϧ 
 public class MainActivity extends AppCompatActivity {
 @Override


    protected void onCreate(Bundle savedInstanceState) { // do something.. GcmNetworkManager gcmNetworkManager = GcmNetworkManager.getInstance(this);
 OneoffTask task = new OneoffTask.Builder()
 .setService(MyTaskService.class)
 .setTag(TASK_TAG)
 .setExecutionWindow(0L, 3600L)
 .setPersisted(true)
 .build();
 gcmNetworkManager.schedule(task); }
 }  0OFPGG5BTL 1FSJPEJD5BTLͷ#VJMEFSΛ࢖ͬͯ5BTLΛ࡞੒͠ɺ (DN/FUXPSL.BOBHFSʹεέδϡʔϧ͢Δ
  16. 'JSFCBTF+PC%JTQBUDIFSαϯϓϧ αʔϏεଆ public class MyJobService extends JobService {
 
 @Override


    public boolean onStartJob(JobParameters job) {
 Log.d("MyJobService", "onStartJob");
 return false;
 }
 
 @Override
 public boolean onStopJob(JobParameters job) {
 return false;
 }
 } <service
 android:name=".MyJobService"
 android:exported="false">
 <intent-filter>
 <action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE" />
 </intent-filter>
 </service>
  17. ͂ 'JSFCBTF+PC%JTQBUDIFSαϯϓϧ εέδϡʔϧ 
 public class MainActivity extends AppCompatActivity {


    
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
 Job myJob = dispatcher.newJobBuilder()
 .addConstraint(Constraint.ON_ANY_NETWORK)
 .setService(MyJobService.class)
 .setTag("my-unique-tag")
 .setLifetime(Lifetime.FOREVER)
 .build();
 dispatcher.mustSchedule(myJob);
 }
 }  'JSFCBTF+PC%JTQBUDIFSOFX+PC#VJMEFSͰɺ+PCΛ࡞੒͠
 NVTU4DIFEVMFPSTDIFEVMFͰεέδϡʔϧ͢Δ
  18. 'JSFCBTF+PC%JTQBUDIFS  ඍົͳ఺!  ϝϯςφϯεੑ͕ͪΐͬͱո͍͠  JTTVF͕์ஔ͞Ε͕ͪ  ΍ͬͱ࠷ۙNBWFOϨϙδτϦʹެ։͞Εͨ 

    ࣮࣭1MBZ4FSWJDFT͕ඞਢͳঢ়ଶ ޙड़   ίʔυ͸ಡΊΔ͕શ༰Λ೺ѲͰ͖ΔΘ͚Ͱ͸ͳ͍
  19. ͜ͷηογϣϯͷ·ͱΊ  "MBSN.BOBHFS Ћ  ࣌ؒʹݫີͳεέδϡʔϦϯά͕͍ͨ͠  +PC4DIFEVMFS  όοςϦʔύϑΥʔϚϯεΛҙ͍ࣝͨ͠

     ଟগ஗Εͯ΋໰୊ͳ͍όονॲཧ  ($.OFUXPSL.BOBHFS  04ҎԼ΋αϙʔτ͍ͨ͠  1MBZ4FSWJDFT͕ೖ͍ͬͯΔ୺຤͚ͩαϙʔτͰ͖Ε͹Α͍
  20. &WFSOPUFBOESPJEKPCͱ͍͏બ୒ࢶ  &WFSOPUF͕͢΂ͯͷҋΛٵऩͯ͠࡞ͬͯ͘Εͨ࠷ڧͷδϣϒ
 εέδϡʔϥʔ$  04Ҏ্Ͱར༻Մೳ  "MBSN.BOBHFS ($./FUXPSL.BOHFS +PC4DIFEVMFSΛ


    όοΫΤϯυͱͯ͠ར༻͍ͯ͠Δ  ϥΠϒϥϦଆ͕࠷దͳόοΫΤϯυΛબ୒͢ΔͷͰ։ൃऀ͕
 Կ͕࢖ΘΕΔ͔ҙࣝ͢Δඞཁ͕ͳ͍ IUUQTCMPHFWFSOPUFDPNUFDIVOJGJFEKPCMJCSBSZBOESPJE
  21. ͦͷଞࢀߟʹ͍͍ͤͯͨͩͨ͞هࣄ  4DIFEVMJOH3FQFBUJOH"MBSNT  EFWFMPQFSBOESPJEDPNUSBJOJOHTDIFEVMJOHBMBSNTIUNM  4DIFEVMJOHKPCTMJLFBQSPXJUI+PC4DIFEVMFS  NFEJVNDPNHPPHMFEFWFMPQFSTFG 

    4BNQMFT+PC4DIFEVMFS  EFWFMPQFSBOESPJEDPNTBNQMFT+PC4DIFEVMFSJOEFYIUNM  *NQMFNFOUJOH($./FUXPSL.BOBHFSPO"OESPJE  EFWFMPQFSTHPPHMFDPNDMPVENFTTBHJOHOFUXPSLNBOBHFS