https://github.com/zapek/workerfragment
https://github.com/zapek/workerfragment
android intentservice
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/zapek/workerfragment
- Owner: zapek
- Created: 2013-06-17T13:40:27.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2013-06-22T08:43:21.000Z (almost 12 years ago)
- Last Synced: 2025-01-28T03:42:58.831Z (4 months ago)
- Topics: android, intentservice
- Language: Java
- Homepage:
- Size: 492 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
WorkerFragment
==============Purpose
-------
WorkerFragment is a mechanism that allows an Activity to start an IntentService and not lose its reply, even
if the Activity is recreated (rotation, etc...).How to use
----------
Simply copy WorkerFragment.java into your project, then on your Activity's onCreate() do this:this.workerFragment = WorkerFragment.getWorkerFragment(this);
If savedInstanceState == null, you start your IntentService to do some background work:
Intent exampleIntent = new Intent(this, ExampleService.class);
exampleIntent.setAction(ExampleService.ACTION_GET_HELLO);
this.workerFragment.putReceiver(exampleIntent); /* this sets the WorkerFragment as receiver */
this.startService(exampleIntent);Then you implement the WorkerFragment.WorkerFragmentReceiver interface in your Activity to handle the result:
@Override
public void onReceiveResult(int resultCode, Bundle resultData) {
switch (resultCode)
{
case WorkerFragment.RESULTCODE_HELLOWORLD:
{
/* do your stuff here */
}
break;
}Now in your IntentService, sending your result is as simple as:
Bundle result = new Bundle();
result.putString(KEY_RESULT, "hello world!");
WorkerFragment.sendResult(intent, WorkerFragment.RESULTCODE_HELLOWORLD, result);