-
Notifications
You must be signed in to change notification settings - Fork 4
Propoid util
Propoid-util offers convenience working with services and content.
TaskService offers an easy management of asynchronous tasks and their observers:
public class FooService extends TaskService<FooObserver> {
// must be public
public class BarTask extends Task {
// must be public, intent or no argument
public class BarTask(Intent intent) {
}
// asynchronously executed
protected void onExecute() {
longRunningExecution();
// initiate publish
publish();
}
// publish on main thread
protected void onPublish(FooObserver observer) {
observer.onBar();
}
}
}To let your activity observe tasks you just have to create a subclass of TaskObserver and subscribe it in onStart():
FooObserver observer = new FooObserver() {
public void onBar() {
}
};
public void onCreate() {
super.onCreate();
observer.subscribe(context, FooService.class);
}To start a task you send an intent to your service:
startService(FooService.createIntent(this, BarTask.class));Don't forget to unsubscribe the observer in onDestroy():
public void onDestroy() {
observer.unsubscribe()
super.onDestroy();
}By default all Tasks are executed in parallel. But you can easily let your tasks include others:
private class BarTask extends Task {
public boolean includes(Task other) {
if (other instanceof BarTask) {
// skip similar tasks
return true;
} else if (other instanceof BazTask) {
// BazTask must run in succession of this task
append(other);
return true;
}
// unrelated, let it be scheduled in parallel
return false;
}
}Preference allows convenient typed access to a preference:
Preference<Boolean> myBoolean = Preference.getBoolean(context, R.string.my_boolean);
Preference<Integer> myInteger = Preference.getInteger(context, R.string.my_integer);Thanks to the keys being declares in your strings.xml we have a build-time safe identification of preferences:
<string name="my_boolean">my_boolean</string>
<string name="my_integer">my_integer</string>
Most of the time (but not necessarily) you want the user to be able to change the preference:
<CheckBoxPreference
android:key="@string/my_boolean"
android:title="@string/my_boolean_title"
android:summary="@string/my_boolean_summary"
android:defaultValue="true"
/>
<EditTextPreference
android:key="@string/my_integer"
android:title="@string/my_integer_title"
android:summary="@string/my_integer_summary"
android:numeric="integer"
android:defaultValue="10"
/>
Note that in the latter case - an EditTextPreference with numeric input - any entered String will automatically be converted to the requested type when accessed via Preference.