ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Android Service
§¬§Ú§â§Ú§Ý§Ý §²§à§Ù§à§Ó
Service - §ï§ä§à §Ü§à§Þ§á§à§ß§Ö§ß§ä §á§â§Ú§Ý§à§Ø§Ö§ß§Ú§ñ,
§Ü§à§ä§à§â§í§Û §Þ§à§Ø§Ö§ä §Ó§í§á§à§Ý§ß§ñ§ä§î §Õ§Ý§Ú§ä§Ö§Ý§î§ß§í§Ö
§à§á§Ö§â§Ñ§è§Ú§Ú §Ó §æ§à§ß§Ö, §Ú §ß§Ö §á§â§Ö§Õ§à§ã§ä§Ñ§Ó§Ý§ñ§Ö§ä
§Ô§â§Ñ§æ§Ú§é§Ö§ã§Ü§à§Ô§à §Ú§ß§ä§Ö§â§æ§Ö§Û§ã§Ñ §Õ§Ý§ñ
§Ó§Ù§Ñ§Ú§Þ§à§Õ§Ö§Û§ã§ä§Ó§Ú§ñ §ã §á§à§Ý§î§Ù§à§Ó§Ñ§ä§Ö§Ý§Ö§Þ
§©§Ñ§Õ§Ñ§é§Ú §Õ§Ý§ñ §â§Ö§ê§Ö§ß§Ú§ñ §ã
§á§à§Þ§à§ë§î§ð Service
? §³§Ö§ä§Ö§Ó§í§Ö §à§á§Ö§â§Ñ§è§Ú§Ú
? §£§Ù§Ñ§Ú§Þ§à§Õ§Ö§Û§ã§ä§Ó§Ú§Ö §ã §ã§Ö§â§Ó§Ö§â§à§Þ
? §©§Ñ§Ô§â§å§Ù§Ü§Ñ §æ§Ñ§Û§Ý§à§Ó
? §µ§á§â§Ñ§Ó§Ý§Ö§ß§Ú§Ö §Þ§Ö§Õ§Ú§Ñ§á§Ý§Ö§Ö§â§à§Þ: §Þ§å§Ù§í§Ü§Ñ, §á§à§Õ§Ü§Ñ§ã§ä§í,
Chromecast
? I/O §à§á§Ö§â§Ñ§è§Ú§Ú
? §£§Ù§Ñ§Ú§Þ§à§Õ§Ö§Û§ã§ä§Ó§Ú§Ö §ã Content Provider
§°§ã§ß§à§Ó§ß§í§Ö §ä§Ú§á§í §ã§Ö§â§Ó§Ú§ã§à§Ó
? Started Service
? Foreground Service
? Bounded Service
? JobScheduler (API 21+) §Ú§Ý§Ú GcmNetworkManager
(API 9+)
Started Service
§¨§Ú§Ù§ß§Ö§ß§ß§í§Û §è§Ú§Ü§Ý
? onCreate()
? onStartCommand(Intent intent, int ?ags, int startId)
? onDestroy()
§±§â§Ú§Þ§Ö§â
public class SampleService extends Service {?
Handler mHandler;?
?
@Override?
public void onCreate() {?
super.onCreate();?
mHandler = new Handler();?
}?
?
@Override?
public void onDestroy() {?
super.onDestroy();?
mHandler = null;?
}?
}
§±§â§Ú§Þ§Ö§â
public class SampleService extends Service {?
¡­?
?
@Override?
public int onStartCommand(@Nullable Intent intent,
int ?ags,
int startId) {?
mHandler.postDelayed(() -> stopSelf(startId), showToast());?
return START_STICKY;?
}?
¡­
}
onStartCommand()
? START_NOT_STICKY?
§¯§Ö §á§Ö§â§Ö§ã§à§Ù§Õ§Ñ§Ö§ä Service
? START_STICKY?
§±§Ö§â§Ö§ã§à§Ù§Õ§Ñ§Ö§ä Service, §ß§à §ß§Ö §Õ§à§ã§ä§Ñ§Ó§Ý§ñ§Ö§ä §á§à§ã§Ý§Ö§Õ§ß§Ú§Û Intent
? START_REDELIVER_INTENT?
§±§Ö§â§Ö§ã§à§Ù§Õ§Ñ§Ö§ä Service §Ú §Õ§à§ã§ä§Ñ§Ó§Ý§ñ§Ö§ä §á§à§ã§Ý§Ö§Õ§ß§Ú§Û Intent
IntentService
public class HelloIntentService extends IntentService {
? public HelloIntentService() {
? ? ? super("HelloIntentService");
? }
? @Override
? protected void onHandleIntent(Intent intent) {
? ? ? try {
? ? ? ? ? Thread.sleep(5000);
? ? ? } catch (InterruptedException e) {
? ? ? ? ? Thread.currentThread().interrupt();
? ? ? }
? }
}
§£§Ñ§Ø§ß§à
? Service §ß§Ö §ã§à§Ù§Õ§Ñ§Ö§ä §à§ä§Õ§Ö§Ý§î§ß§í§ç §á§à§ä§à§Ü§à§Ó §Ú§Ý§Ú
§á§â§à§è§Ö§ã§ã§à§Ó
? §ª§ã§á§à§Ý§î§Ù§å§Û§ä§Ö WakeLock
? §¯§Ö §Ù§Ñ§Ò§í§Ó§Ñ§Û§ä§Ö §à§ã§ä§Ñ§ß§Ñ§Ó§Ý§Ú§Ó§Ñ§ä§î Service
? §°§ã§ä§Ñ§ß§Ñ§Ó§Ý§Ú§Ó§Ñ§Û§ä§Ö Service §Þ§Ö§ä§à§Õ§à§Þ stopSelfResult(int
startId), §Ó§Þ§Ö§ã§ä§à stopSelf()
? §¥§à§Ò§Ñ§Ó§Ý§ñ§Û§ä§Ö §à§á§Ú§ã§Ñ§ß§Ú§Ö §Ó§Ñ§ê§Ö§Þ§å Service §Ó Android
Manifest
Bounded Service
§¨§Ú§Ù§ß§Ö§ß§ß§í§Û §è§Ú§Ü§Ý
? onCreate()
? onBind(Intent intent)
? onRebind(Intent intent)
? onUnbind(Intent intent)
? onDestroy()
Android service
§²§Ö§Ñ§Ý§Ú§Ù§Ñ§è§Ú§ñ IBinder
? §­§à§Ü§Ñ§Ý§î§ß§à§Ö §Ú§ã§á§à§Ý§î§Ù§à§Ó§Ñ§ß§Ú§Ö
? ¡­ extends Binder
? §®§Ö§Ø§á§â§à§è§Ö§ã§ã§ß§à§Ö §Ó§Ù§Ñ§Ú§Þ§à§Õ§Ö§Û§ã§ä§Ó§Ú§Ö
? Messenger
? AIDL
§±§â§Ú§Þ§Ö§â
public class LocalService extends Service {
? ? private ?nal IBinder mBinder = new LocalBinder();
? ? public class LocalBinder extends Binder {
? ? ? ? LocalService getService() {
? ? ? ? ? ? return LocalService.this;
? ? ? ? }
? ? }
? ? @Override
? ? public IBinder onBind(Intent intent) {
? ? ? ? return mBinder;
? ? }
¡­
}
public class BindingActivity extends Activity {
? ? LocalService mService;
? ? boolean mBound = false;
? ? private ?nal ServiceConnection mConnection = new LocalServiceConnection();
? ? @Override
? ? protected void onStart() {
? ? ? ? super.onStart();
? ? ? ? bindService(new Intent(this, LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
? ? }
? ? @Override
? ? protected void onStop() {
? ? ? ? super.onStop();
? ? ? ? if (mBound) {
? ? ? ? ? ? unbindService(mConnection);
? ? ? ? ? ? mBound = false;
? ? ? ? }
? ? }
? ? private ?nal class LocalServiceConnection implements ServiceConnection {
? ? ? ? @Override
? ? ? ? public void onServiceConnected(ComponentName className, IBinder service) {
? ? ? ? ? ? LocalBinder binder = (LocalBinder) service;
? ? ? ? ? ? mService = binder.getService();
? ? ? ? ? ? mBound = true;
? ? ? ? }
? ? ? ? @Override
? ? ? ? public void onServiceDisconnected(ComponentName service) {
? ? ? ? ? ? mBound = false;
? ? ? ? }
? ? };
}
§£§Ñ§Ø§ß§à
? §¥§Ö§Ý§Ñ§ä§î unbindService() §Õ§Ý§ñ §Ü§Ñ§Ø§Õ§à§Ô§à bindService()
? §ª§ã§á§à§Ý§î§Ù§å§Û§ä§Ö §ä§à§Ý§î§Ü§à §ñ§Ó§ß§í§Ö Intent §Ó bindService()
? Binding §ã Service §Þ§à§Ø§ß§à §Ó§í§á§à§Ý§ß§Ú§ä§î §ä§à§Ý§î§Ü§à §Ú§Ù
Activity, Serivce §Ú Content Provider
§¨§Ú§Ù§ß§Ö§ß§ß§í§Û §è§Ú§Ü§Ý
Foreground Service
public class SampleForegroundService extends Service {?
@Override?
public void onCreate() {?
super.onCreate();?
Noti?cationCompat.Builder builder = new Noti?cationCompat.Builder(this)?
.setContentTitle("Sample foreground service")?
.setContentTitle("Progress running")?
.setProgress(0, 1, true)?
.setSmallIcon(R.mipmap.ic_no?ciation);?
Noti?cation noti?cation = builder.build();?
startForeground(FOREGROUND_NOTIFICATION_ID, noti?cation);?
}
¡­?
}
Android service
Job Scheduler
§£§à§Ù§Þ§à§Ø§ß§à§ã§ä§Ú JobScheduler
? §°§ä§Ý§à§Ø§Ö§ß§ß§í§Û §Ù§Ñ§á§å§ã§Ü §Ù§Ñ§Õ§Ñ§é §ß§Ñ §à§ã§ß§à§Ó§Ö §Ù§Ñ§Õ§Ñ§ß§ß§í§ç
§Ü§â§Ú§ä§Ö§â§Ú§Ö§Ó
? §±§Ö§â§Ú§à§Õ§Ú§é§Ö§ã§Ü§à§Ö §Ó§í§á§à§Ý§ß§Ö§ß§Ú§Ö
? §²§Ñ§Ò§à§ä§í §Ó Doze Mode
? §¤§â§å§á§á§Ú§â§à§Ó§Ü§Ñ §Ù§Ñ§á§å§ã§Ü§Ñ §Ù§Ñ§Õ§Ñ§é
? §©§Ñ§Þ§Ö§ß§Ñ CONNECTIVITY_ACTION broadcast,
§â§Ö§Ô§Ú§ã§ä§â§Ú§â§å§Ö§Þ§í§Û §é§Ö§â§Ö§Ù AndroidManifest
§±§â§Ú§Þ§Ö§â
ComponentName jobServiceComponent =
new ComponentName(this, JobSchedulerService.class);
JobInfo.Builder builder =?
new JobInfo.Builder(SAMPLE_JOB_ID, jobServiceComponent)?
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)?
.setPersisted(false)?
.setRequiresDeviceIdle(false)?
.setRequiresCharging(false)?
.setOverrideDeadline(1_000);
§±§â§Ú§Þ§Ö§â
JobScheduler jobScheduler = (JobScheduler)
getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(builder.build());
§±§â§Ú§Þ§Ö§â
public class JobSchedulerService extends JobService {?
?
public static ?nal int SAMPLE_JOB_ID = 1000;?
?
@Override?
public boolean onStartJob(JobParameters params) {
if (params.getJobId() == Constants.SAMPLE_JOB_ID) {?
Handler handler = new Handler();?
handler.postDelayed(() -> jobFinished(params, false), duration);?
}
return false;
}
@Override
public boolean onStopJob(JobParameters params) {?
return false;
}
}
AndroidManifest.xml
<service?
android:name=¡°.JobSchedulerService"
android:exported=¡°true¡±?
android:permission="android.permission.BIND_JOB_SERVICE"?
/>
§µ§ã§Ý§à§Ó§Ú§ñ JobScheduler
? §©§Ñ§â§ñ§Õ§Ü§Ñ
? §´§Ú§á §ã§Ö§ä§Ú
? NETWORK_TYPE_NONE (§á§à §å§Þ§à§Ý§é§Ñ§ß§Ú§ð)
? NETWORK_TYPE_ANY
? NETWORK_TYPE_NOT_ROAMING (API 24+)
? NETWORK_TYPE_UNMETERED
? §µ§ã§ä§â§à§Û§ã§ä§Ó§à §ß§Ñ§ç§à§Õ§Ú§ä§ã§ñ §Ó §á§â§à§ã§ä§à§Ö
GcmNetworkManager
§±§â§Ú§Þ§Ö§â
public class TaskSampleService extends GcmTaskService {?
?
@GcmTaskResultDef?
@Override?
public int onRunTask(TaskParams taskParams) {?
if (SAMPLE_JOB_TAG.equals(taskParams.getTag())) {?
// Do long work. Execute on background thread by default.?
}?
return GcmNetworkManager.RESULT_SUCCESS;?
}?
}
AndroidManifest.xml
<service
android:name=¡°.TaskSampleService"
? ? ?android:exported="true"
? ? ?android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE">
? ? ? ? ?<intent-?lter>
? ? ? ? ? ? ? <action android:name="com.google.android.gms.gcm.ACTION_TASK_READY"/>
? ? ? ? ?</intent-?lter>
</service>
§±§â§Ú§Þ§Ö§â
public class GcmNetworkManagerService extends GcmTaskService {?
?
@Override?
public void onInitializeTasks() {?
// Restore tasks after application package updated.
// Not called on ?rst start.
}?
}
§±§â§Ú§Þ§Ö§â
OneoffTask.Builder builder = new OneoffTask.Builder();?
builder.setService(GcmTaskService.class)?
.setRequiredNetwork(Task.NETWORK_STATE_ANY)?
.setPersisted(true)?
.setRequiresCharging(false)?
.setExecutionWindow(0, TimeUnit.SECONDS.toMillis(1));?
OneoffTask task = builder.build();?
?
GcmNetworkManager.getInstance(this) .schedule(task);
§µ§ã§Ý§à§Ó§Ú§ñ
GcmNetworkManager
? §©§Ñ§â§ñ§Õ§Ü§Ñ
? §´§Ú§á §ã§Ö§ä§Ú
? NETWORK_STATE_ANY
? NETWORK_STATE_CONNECTED
? NETWORK_STATE_UNMETERED
§°§ä§Ý§Ú§é§Ú§ñ JobScheduler §Ú
GcmNetworkManager
? §¥§Ý§ñ §â§Ñ§Ò§à§ä§í GcmNetworkManager §à§Ò§ñ§Ù§Ñ§ä§Ö§Ý§î§ß§à §ß§Ö§à§Ò§ç§à§Õ§Ú§Þ§à
§ß§Ñ§Ý§Ú§é§Ú§Ö Google Play Service §ß§Ñ §å§ã§ä§â§à§Û§ã§ä§Ó§Ö §Ó §â§Ñ§Ò§à§é§Ö§Þ §ã§à§ã§ä§à§ñ§ß§Ú§Ö
? GcmNetworkManager §Ù§Ñ§ç§Ó§Ñ§ä§í§Ó§Ñ§Ö§ä §Ò§Ý§à§Ü§Ú§â§à§Ó§Ü§å
PowerManager.WakeLock §Õ§Ý§ñ §Ó§Ñ§ê§Ö§Ô§à Service, §Ú §Ó§Ñ§ê§Ñ §Ù§Ñ§Õ§Ñ§é§Ñ
§Ú§Þ§Ö§Ö§ä 3 §Þ§Ú§ß§å§ä§í §ß§Ñ §Ó§í§á§à§Ý§ß§Ö§ß§Ú§Ö. §£ §á§â§à§ä§Ú§Ó§ß§à§Þ §ã§Ý§å§é§Ñ§Ö WakeLock
§Ò§å§Õ§Ö§ä §ã§ß§ñ§ä §Ú §á§Ö§â§Ö§Ù§Ñ§á§å§ã§Ü §Ù§Ñ§Õ§Ñ§é§Ú §ß§Ö §Ò§å§Õ§Ö§ä §Ú§Þ§Ö§ä§î §ï§æ§æ§Ö§Ü§ä§Ñ.
? §±§â§Ú §à§Ò§ß§à§Ó§Ý§Ö§ß§Ú§Ö §Ú§Ý§Ú §å§Õ§Ñ§Ý§Ö§ß§Ú§Ö §Ó§Ñ§ê§Ö§Ô§à §á§â§Ú§Ý§à§Ø§Ö§ß§Ú§ñ
GcmNetworkManager §Þ§à§Ø§Ö§ä §ã§Ò§â§Ñ§ã§í§Ó§Ñ§Ö§ä §Ó§ã§ð §à§é§Ö§â§Ö§Õ§î §ä§Ñ§ã§Ü§à§Ó §Ú
§Ö§× §ß§å§Ø§ß§à §Ó§à§ã§ã§ä§Ñ§ß§Ñ§Ó§Ý§Ú§Ó§Ñ§ä§î
? §¥§Ý§ñ §á§à§Ó§ä§à§â§ñ§ð§ë§Ú§ç§ã§ñ §Ù§Ñ§Õ§Ñ§é §Ó GcmNetworkManager §Ú§ã§á§à§Ý§î§Ù§å§Ö§ä§ã§ñ
§à§ä§Õ§Ö§Ý§î§ß§í§Û §Ü§Ý§Ñ§ã§ã - PeriodicTask
§±§à§Ý§Ö§Ù§ß§í§Ö §ã§Ö§â§Ó§Ú§ã§í
? DownloadManager
? Android Upload Service (github.com/gotev/
android-upload-service)
§³§á§Ñ§ã§Ú§Ò§à §Ù§Ñ §Ó§ß§Ú§Þ§Ñ§ß§Ú§Ö

More Related Content

Android service

  • 2. Service - §ï§ä§à §Ü§à§Þ§á§à§ß§Ö§ß§ä §á§â§Ú§Ý§à§Ø§Ö§ß§Ú§ñ, §Ü§à§ä§à§â§í§Û §Þ§à§Ø§Ö§ä §Ó§í§á§à§Ý§ß§ñ§ä§î §Õ§Ý§Ú§ä§Ö§Ý§î§ß§í§Ö §à§á§Ö§â§Ñ§è§Ú§Ú §Ó §æ§à§ß§Ö, §Ú §ß§Ö §á§â§Ö§Õ§à§ã§ä§Ñ§Ó§Ý§ñ§Ö§ä §Ô§â§Ñ§æ§Ú§é§Ö§ã§Ü§à§Ô§à §Ú§ß§ä§Ö§â§æ§Ö§Û§ã§Ñ §Õ§Ý§ñ §Ó§Ù§Ñ§Ú§Þ§à§Õ§Ö§Û§ã§ä§Ó§Ú§ñ §ã §á§à§Ý§î§Ù§à§Ó§Ñ§ä§Ö§Ý§Ö§Þ
  • 3. §©§Ñ§Õ§Ñ§é§Ú §Õ§Ý§ñ §â§Ö§ê§Ö§ß§Ú§ñ §ã §á§à§Þ§à§ë§î§ð Service ? §³§Ö§ä§Ö§Ó§í§Ö §à§á§Ö§â§Ñ§è§Ú§Ú ? §£§Ù§Ñ§Ú§Þ§à§Õ§Ö§Û§ã§ä§Ó§Ú§Ö §ã §ã§Ö§â§Ó§Ö§â§à§Þ ? §©§Ñ§Ô§â§å§Ù§Ü§Ñ §æ§Ñ§Û§Ý§à§Ó ? §µ§á§â§Ñ§Ó§Ý§Ö§ß§Ú§Ö §Þ§Ö§Õ§Ú§Ñ§á§Ý§Ö§Ö§â§à§Þ: §Þ§å§Ù§í§Ü§Ñ, §á§à§Õ§Ü§Ñ§ã§ä§í, Chromecast ? I/O §à§á§Ö§â§Ñ§è§Ú§Ú ? §£§Ù§Ñ§Ú§Þ§à§Õ§Ö§Û§ã§ä§Ó§Ú§Ö §ã Content Provider
  • 4. §°§ã§ß§à§Ó§ß§í§Ö §ä§Ú§á§í §ã§Ö§â§Ó§Ú§ã§à§Ó ? Started Service ? Foreground Service ? Bounded Service ? JobScheduler (API 21+) §Ú§Ý§Ú GcmNetworkManager (API 9+)
  • 6. §¨§Ú§Ù§ß§Ö§ß§ß§í§Û §è§Ú§Ü§Ý ? onCreate() ? onStartCommand(Intent intent, int ?ags, int startId) ? onDestroy()
  • 7. §±§â§Ú§Þ§Ö§â public class SampleService extends Service {? Handler mHandler;? ? @Override? public void onCreate() {? super.onCreate();? mHandler = new Handler();? }? ? @Override? public void onDestroy() {? super.onDestroy();? mHandler = null;? }? }
  • 8. §±§â§Ú§Þ§Ö§â public class SampleService extends Service {? ¡­? ? @Override? public int onStartCommand(@Nullable Intent intent, int ?ags, int startId) {? mHandler.postDelayed(() -> stopSelf(startId), showToast());? return START_STICKY;? }? ¡­ }
  • 9. onStartCommand() ? START_NOT_STICKY? §¯§Ö §á§Ö§â§Ö§ã§à§Ù§Õ§Ñ§Ö§ä Service ? START_STICKY? §±§Ö§â§Ö§ã§à§Ù§Õ§Ñ§Ö§ä Service, §ß§à §ß§Ö §Õ§à§ã§ä§Ñ§Ó§Ý§ñ§Ö§ä §á§à§ã§Ý§Ö§Õ§ß§Ú§Û Intent ? START_REDELIVER_INTENT? §±§Ö§â§Ö§ã§à§Ù§Õ§Ñ§Ö§ä Service §Ú §Õ§à§ã§ä§Ñ§Ó§Ý§ñ§Ö§ä §á§à§ã§Ý§Ö§Õ§ß§Ú§Û Intent
  • 10. IntentService public class HelloIntentService extends IntentService { ? public HelloIntentService() { ? ? ? super("HelloIntentService"); ? } ? @Override ? protected void onHandleIntent(Intent intent) { ? ? ? try { ? ? ? ? ? Thread.sleep(5000); ? ? ? } catch (InterruptedException e) { ? ? ? ? ? Thread.currentThread().interrupt(); ? ? ? } ? } }
  • 11. §£§Ñ§Ø§ß§à ? Service §ß§Ö §ã§à§Ù§Õ§Ñ§Ö§ä §à§ä§Õ§Ö§Ý§î§ß§í§ç §á§à§ä§à§Ü§à§Ó §Ú§Ý§Ú §á§â§à§è§Ö§ã§ã§à§Ó ? §ª§ã§á§à§Ý§î§Ù§å§Û§ä§Ö WakeLock ? §¯§Ö §Ù§Ñ§Ò§í§Ó§Ñ§Û§ä§Ö §à§ã§ä§Ñ§ß§Ñ§Ó§Ý§Ú§Ó§Ñ§ä§î Service ? §°§ã§ä§Ñ§ß§Ñ§Ó§Ý§Ú§Ó§Ñ§Û§ä§Ö Service §Þ§Ö§ä§à§Õ§à§Þ stopSelfResult(int startId), §Ó§Þ§Ö§ã§ä§à stopSelf() ? §¥§à§Ò§Ñ§Ó§Ý§ñ§Û§ä§Ö §à§á§Ú§ã§Ñ§ß§Ú§Ö §Ó§Ñ§ê§Ö§Þ§å Service §Ó Android Manifest
  • 13. §¨§Ú§Ù§ß§Ö§ß§ß§í§Û §è§Ú§Ü§Ý ? onCreate() ? onBind(Intent intent) ? onRebind(Intent intent) ? onUnbind(Intent intent) ? onDestroy()
  • 15. §²§Ö§Ñ§Ý§Ú§Ù§Ñ§è§Ú§ñ IBinder ? §­§à§Ü§Ñ§Ý§î§ß§à§Ö §Ú§ã§á§à§Ý§î§Ù§à§Ó§Ñ§ß§Ú§Ö ? ¡­ extends Binder ? §®§Ö§Ø§á§â§à§è§Ö§ã§ã§ß§à§Ö §Ó§Ù§Ñ§Ú§Þ§à§Õ§Ö§Û§ã§ä§Ó§Ú§Ö ? Messenger ? AIDL
  • 16. §±§â§Ú§Þ§Ö§â public class LocalService extends Service { ? ? private ?nal IBinder mBinder = new LocalBinder(); ? ? public class LocalBinder extends Binder { ? ? ? ? LocalService getService() { ? ? ? ? ? ? return LocalService.this; ? ? ? ? } ? ? } ? ? @Override ? ? public IBinder onBind(Intent intent) { ? ? ? ? return mBinder; ? ? } ¡­ }
  • 17. public class BindingActivity extends Activity { ? ? LocalService mService; ? ? boolean mBound = false; ? ? private ?nal ServiceConnection mConnection = new LocalServiceConnection(); ? ? @Override ? ? protected void onStart() { ? ? ? ? super.onStart(); ? ? ? ? bindService(new Intent(this, LocalService.class), mConnection, Context.BIND_AUTO_CREATE); ? ? } ? ? @Override ? ? protected void onStop() { ? ? ? ? super.onStop(); ? ? ? ? if (mBound) { ? ? ? ? ? ? unbindService(mConnection); ? ? ? ? ? ? mBound = false; ? ? ? ? } ? ? } ? ? private ?nal class LocalServiceConnection implements ServiceConnection { ? ? ? ? @Override ? ? ? ? public void onServiceConnected(ComponentName className, IBinder service) { ? ? ? ? ? ? LocalBinder binder = (LocalBinder) service; ? ? ? ? ? ? mService = binder.getService(); ? ? ? ? ? ? mBound = true; ? ? ? ? } ? ? ? ? @Override ? ? ? ? public void onServiceDisconnected(ComponentName service) { ? ? ? ? ? ? mBound = false; ? ? ? ? } ? ? }; }
  • 18. §£§Ñ§Ø§ß§à ? §¥§Ö§Ý§Ñ§ä§î unbindService() §Õ§Ý§ñ §Ü§Ñ§Ø§Õ§à§Ô§à bindService() ? §ª§ã§á§à§Ý§î§Ù§å§Û§ä§Ö §ä§à§Ý§î§Ü§à §ñ§Ó§ß§í§Ö Intent §Ó bindService() ? Binding §ã Service §Þ§à§Ø§ß§à §Ó§í§á§à§Ý§ß§Ú§ä§î §ä§à§Ý§î§Ü§à §Ú§Ù Activity, Serivce §Ú Content Provider
  • 20. Foreground Service public class SampleForegroundService extends Service {? @Override? public void onCreate() {? super.onCreate();? Noti?cationCompat.Builder builder = new Noti?cationCompat.Builder(this)? .setContentTitle("Sample foreground service")? .setContentTitle("Progress running")? .setProgress(0, 1, true)? .setSmallIcon(R.mipmap.ic_no?ciation);? Noti?cation noti?cation = builder.build();? startForeground(FOREGROUND_NOTIFICATION_ID, noti?cation);? } ¡­? }
  • 23. §£§à§Ù§Þ§à§Ø§ß§à§ã§ä§Ú JobScheduler ? §°§ä§Ý§à§Ø§Ö§ß§ß§í§Û §Ù§Ñ§á§å§ã§Ü §Ù§Ñ§Õ§Ñ§é §ß§Ñ §à§ã§ß§à§Ó§Ö §Ù§Ñ§Õ§Ñ§ß§ß§í§ç §Ü§â§Ú§ä§Ö§â§Ú§Ö§Ó ? §±§Ö§â§Ú§à§Õ§Ú§é§Ö§ã§Ü§à§Ö §Ó§í§á§à§Ý§ß§Ö§ß§Ú§Ö ? §²§Ñ§Ò§à§ä§í §Ó Doze Mode ? §¤§â§å§á§á§Ú§â§à§Ó§Ü§Ñ §Ù§Ñ§á§å§ã§Ü§Ñ §Ù§Ñ§Õ§Ñ§é ? §©§Ñ§Þ§Ö§ß§Ñ CONNECTIVITY_ACTION broadcast, §â§Ö§Ô§Ú§ã§ä§â§Ú§â§å§Ö§Þ§í§Û §é§Ö§â§Ö§Ù AndroidManifest
  • 24. §±§â§Ú§Þ§Ö§â ComponentName jobServiceComponent = new ComponentName(this, JobSchedulerService.class); JobInfo.Builder builder =? new JobInfo.Builder(SAMPLE_JOB_ID, jobServiceComponent)? .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)? .setPersisted(false)? .setRequiresDeviceIdle(false)? .setRequiresCharging(false)? .setOverrideDeadline(1_000);
  • 25. §±§â§Ú§Þ§Ö§â JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); jobScheduler.schedule(builder.build());
  • 26. §±§â§Ú§Þ§Ö§â public class JobSchedulerService extends JobService {? ? public static ?nal int SAMPLE_JOB_ID = 1000;? ? @Override? public boolean onStartJob(JobParameters params) { if (params.getJobId() == Constants.SAMPLE_JOB_ID) {? Handler handler = new Handler();? handler.postDelayed(() -> jobFinished(params, false), duration);? } return false; } @Override public boolean onStopJob(JobParameters params) {? return false; } }
  • 28. §µ§ã§Ý§à§Ó§Ú§ñ JobScheduler ? §©§Ñ§â§ñ§Õ§Ü§Ñ ? §´§Ú§á §ã§Ö§ä§Ú ? NETWORK_TYPE_NONE (§á§à §å§Þ§à§Ý§é§Ñ§ß§Ú§ð) ? NETWORK_TYPE_ANY ? NETWORK_TYPE_NOT_ROAMING (API 24+) ? NETWORK_TYPE_UNMETERED ? §µ§ã§ä§â§à§Û§ã§ä§Ó§à §ß§Ñ§ç§à§Õ§Ú§ä§ã§ñ §Ó §á§â§à§ã§ä§à§Ö
  • 30. §±§â§Ú§Þ§Ö§â public class TaskSampleService extends GcmTaskService {? ? @GcmTaskResultDef? @Override? public int onRunTask(TaskParams taskParams) {? if (SAMPLE_JOB_TAG.equals(taskParams.getTag())) {? // Do long work. Execute on background thread by default.? }? return GcmNetworkManager.RESULT_SUCCESS;? }? }
  • 31. AndroidManifest.xml <service android:name=¡°.TaskSampleService" ? ? ?android:exported="true" ? ? ?android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE"> ? ? ? ? ?<intent-?lter> ? ? ? ? ? ? ? <action android:name="com.google.android.gms.gcm.ACTION_TASK_READY"/> ? ? ? ? ?</intent-?lter> </service>
  • 32. §±§â§Ú§Þ§Ö§â public class GcmNetworkManagerService extends GcmTaskService {? ? @Override? public void onInitializeTasks() {? // Restore tasks after application package updated. // Not called on ?rst start. }? }
  • 33. §±§â§Ú§Þ§Ö§â OneoffTask.Builder builder = new OneoffTask.Builder();? builder.setService(GcmTaskService.class)? .setRequiredNetwork(Task.NETWORK_STATE_ANY)? .setPersisted(true)? .setRequiresCharging(false)? .setExecutionWindow(0, TimeUnit.SECONDS.toMillis(1));? OneoffTask task = builder.build();? ? GcmNetworkManager.getInstance(this) .schedule(task);
  • 34. §µ§ã§Ý§à§Ó§Ú§ñ GcmNetworkManager ? §©§Ñ§â§ñ§Õ§Ü§Ñ ? §´§Ú§á §ã§Ö§ä§Ú ? NETWORK_STATE_ANY ? NETWORK_STATE_CONNECTED ? NETWORK_STATE_UNMETERED
  • 35. §°§ä§Ý§Ú§é§Ú§ñ JobScheduler §Ú GcmNetworkManager ? §¥§Ý§ñ §â§Ñ§Ò§à§ä§í GcmNetworkManager §à§Ò§ñ§Ù§Ñ§ä§Ö§Ý§î§ß§à §ß§Ö§à§Ò§ç§à§Õ§Ú§Þ§à §ß§Ñ§Ý§Ú§é§Ú§Ö Google Play Service §ß§Ñ §å§ã§ä§â§à§Û§ã§ä§Ó§Ö §Ó §â§Ñ§Ò§à§é§Ö§Þ §ã§à§ã§ä§à§ñ§ß§Ú§Ö ? GcmNetworkManager §Ù§Ñ§ç§Ó§Ñ§ä§í§Ó§Ñ§Ö§ä §Ò§Ý§à§Ü§Ú§â§à§Ó§Ü§å PowerManager.WakeLock §Õ§Ý§ñ §Ó§Ñ§ê§Ö§Ô§à Service, §Ú §Ó§Ñ§ê§Ñ §Ù§Ñ§Õ§Ñ§é§Ñ §Ú§Þ§Ö§Ö§ä 3 §Þ§Ú§ß§å§ä§í §ß§Ñ §Ó§í§á§à§Ý§ß§Ö§ß§Ú§Ö. §£ §á§â§à§ä§Ú§Ó§ß§à§Þ §ã§Ý§å§é§Ñ§Ö WakeLock §Ò§å§Õ§Ö§ä §ã§ß§ñ§ä §Ú §á§Ö§â§Ö§Ù§Ñ§á§å§ã§Ü §Ù§Ñ§Õ§Ñ§é§Ú §ß§Ö §Ò§å§Õ§Ö§ä §Ú§Þ§Ö§ä§î §ï§æ§æ§Ö§Ü§ä§Ñ. ? §±§â§Ú §à§Ò§ß§à§Ó§Ý§Ö§ß§Ú§Ö §Ú§Ý§Ú §å§Õ§Ñ§Ý§Ö§ß§Ú§Ö §Ó§Ñ§ê§Ö§Ô§à §á§â§Ú§Ý§à§Ø§Ö§ß§Ú§ñ GcmNetworkManager §Þ§à§Ø§Ö§ä §ã§Ò§â§Ñ§ã§í§Ó§Ñ§Ö§ä §Ó§ã§ð §à§é§Ö§â§Ö§Õ§î §ä§Ñ§ã§Ü§à§Ó §Ú §Ö§× §ß§å§Ø§ß§à §Ó§à§ã§ã§ä§Ñ§ß§Ñ§Ó§Ý§Ú§Ó§Ñ§ä§î ? §¥§Ý§ñ §á§à§Ó§ä§à§â§ñ§ð§ë§Ú§ç§ã§ñ §Ù§Ñ§Õ§Ñ§é §Ó GcmNetworkManager §Ú§ã§á§à§Ý§î§Ù§å§Ö§ä§ã§ñ §à§ä§Õ§Ö§Ý§î§ß§í§Û §Ü§Ý§Ñ§ã§ã - PeriodicTask
  • 36. §±§à§Ý§Ö§Ù§ß§í§Ö §ã§Ö§â§Ó§Ú§ã§í ? DownloadManager ? Android Upload Service (github.com/gotev/ android-upload-service)