1. Serving your business
logically
Dmitry Malykhanov
DataArt
The Android robot is reproduced or modified from work created and shared by Google and used according to terms described in the
Creative Commons 3.0 Attribution License.
2. First of all
why we need services
How is it done
service variants
Then
start execution
Expedited delivery
receiving your data
4. Alternatives
"Just" Code: Service:
● Thread (pool)
○ return to UI thread
● AsyncTask
○ serialized
○ short lifespan
● Application
?
○ garbage on the heap
○ no IPC
5. Alternatives
"Just" Code: Service:
● Thread (pool)
○ return to UI thread
● AsyncTask
○ serialized
○ short lifespan
● Application
?
○ garbage on the heap
○ no IPC
6. Alternatives
"Just" Code: Service:
● Thread (pool)
○ return to UI thread
● AsyncTask
○ serialized
○ short lifespan
● Application
?
○ garbage on the heap
○ no IPC
7. Alternatives
"Just" Code: Service:
● Thread (pool) ● Thread/Pool/Etc.
○ return to UI thread ○ flexible control
● AsyncTask ● Wake Locks
○ serialized ○ guaranteed CPU
○ short lifespan ● Lifetime
● Application ○ on its own
○ garbage on the heap
○ no IPC
8. Alternatives
"Just" Code: Service:
● Thread (pool) ● Thread/Pool/Etc.
○ return to UI thread ○ flexible control
● AsyncTask ● Wake Locks
○ serialized ○ guaranteed CPU
○ short lifespan ● Lifetime
● Application ○ on its own
○ garbage on the heap
○ no IPC
9. Alternatives
"Just" Code: Service:
● Thread (pool) ● Thread/Pool/Etc.
○ return to UI thread ○ flexible control
● AsyncTask ● Wake Locks
○ serialized ○ guaranteed CPU
○ short lifespan ● Lifetime
● Application ○ on its own
○ garbage on the heap
○ no IPC
19. What and how?
switch (cmdId) {
case CMD_POLL:
poll(); How to expand?
break;
case CMD_UPLOAD:
upload();
break;
if ("poll".equals(cmd)) {
poll();
} else if ("upload".equals(cmd)) {
upload();
}
21. Unique IDs
public static final int CMD_POLL = 10;
public static final int CMD_UPLOAD = 20;
...
Command.class.hashCode()
System.identityHashCode()
22. Commands delivery
switch (commandId) {
case CMD_POLL:
return new PollCommand().execute();
case CMD_UPLOAD:
return new UploadCommand().execute();
...
abstract class Command
implements Parcelable {
public abstract void execute(...);
}
23. Out of band execution
Serialize/prioritize commands, e.g.:
Command.isSerial()
frameworks/base/core/java/android/os/AsyncTask.java:
private static class SerialExecutor
implements Executor {
...
44. Serving your business
logically
The Android robot is reproduced or modified from work created and shared by Google and used according to terms described in the
Creative Commons 3.0 Attribution License.