Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Blake Meike
Developer, Architect, Android Evangelist,
Founding member of Twitter University
Testing!
The tools to test Android have gotten pretty
good, in the last 10 months
They continue to improve rapidly!
Periodic tasks
Yes!
Yes
1 minute < period < 1 day
A single code path executed multiple times
No
Loading avatars for a ListView (Volley)
Populating a ListView from a database (Loader)
Receiving a system status update (Receiver)
Hard to do well
All the usual warnings apply!
We need frameworks that make it easy to
do the right things and hard to do the the
wrong things.
Wish list
Thread-safe
Testable
Thread safe
Java thread safety is hard and boring:
It is a perfect job for a computer.
Lifecyle
aware
Smart
scheduling
Thrifty
w/power
TimerTask
Use a Java TimerTask to schedule periodic
execution.
TimerTask
Dead, dead, dead, dead.
Lifecyle
aware
Smart
scheduling
Thrifty
power
Looper/Handler
Use sendMessageDelayed to post a
message for execution at a future time.
Looper/Handler: hot
Looper/Handler: not
A bit bulky
Utterly ephemeral
Lifecyle
aware
Smart
scheduling
Thrifty
Power
@Override
protected void onHandleIntent(Intent intent) {
int op = intent.getIntExtra(PARAM_OP, 0);
switch(op) {
case TASK_1:
helper.doTask1();
break;
case TASK_2:
helper.doTask2(
intent.getStringExtra(PARAM_ARG));
break;
default:
Log.d(TAG, "Unrecognized op: " + op);
}
}
IntentService: details
AlarmManager: details
Alarm persistence
WakefulBroadcastReceiver
Alarm/IntentService: hot
Alarm/IntentService: not
Isolated!
Wakelock management
Lifecyle
aware
Smart
scheduling
Thrifty
Power
Sync Adapter
Connect a local dataset, named by a
content:// url and a credentialed
remote account.
<sync-adapter
xmlns:android
="http://schemas.android.com/apk/res/android"
android:accountType="@string/account_type"
android:contentAuthority
="com.twitter.university.android.minisync"
android:isAlwaysSyncable="true" />
@Override
public void onPerformSync(
Account account,
Bundle bundle,
String s,
ContentProviderClient contentProviderClient,
SyncResult syncResult)
{
doPeriodicTask(account, bundle)
}
Periodically: addPeriodicSync
SyncAdapter: hot
SyncAdapter: not
Quite heavyweight
Requires an Account
SyncAdapter: hot
If you havent noticed
Sync Adapters are actually pretty hot
Lifecyle
aware
Smart
scheduling
Thrifty
Power
Lollypop JobScheduler
A Job is a task to be completed under
specified conditions.
<service
android:name=".svc.YambaScheduler"
android:permission
="android.permission.BIND_JOB_SERVICE"/>
((JobScheduler) ctxt.getSystemService(
Context.JOB_SCHEDULER_SERVICE)).schedule(job);
JobScheduler: architecture
Latency (jitter)
Deadline
Simple to use
Lifecyle
aware
Smart
scheduling
Thrifty
Power
So?
No silver bullets yet.
The JobSchedule is one more tool for
one more set of special cases.
We have yet to achieve a unified theory
of periodic tasks
Whats next?
There is a rumor that SyncAdapters will be
re-implemented on top of JobService. That
would be good.
Random musings.
What about a pattern matcher:
unmetered && elapsed_time > 10m: task1, task2
metered && elapsed_time > 30m: task1
any_net && local_time = 00:00: task2
radio_on && last_post > 1h: task1, task2
Thank you!