My first solution was to use callback methods with an interface implementation in the fooobar.com/questions/1358363 / ... example.
After chatting on Android chat, I heard that there is a more important solution.
You can use IntentService in combination with PendingIntent.
Communication is done using Intent's.
If you want to use the ProgressDialog, for this you need your own action, which registers, for example, BroadcastReciever, and the IntentService sends its actual status for the broadcast.
But let's get started now. First we create an Activity that contains a ProgressDialog and a registered BroadcastReceiver. BroadcastReceiver listens for update and completion messages.
For Activity, we need a layout ...
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#80000000"> </LinearLayout>
... and related code:
public class ProgressActivity extends Activity { private ProgressDialog progessDialog_g; private BroadcastReceiver receiver_g; public static final String PROGRESS_DIALOG_BOOL_HORIZONTAL_BAR = "pbar_horizontal_bar"; public static final String PROGRESS_DIALOG_BOOL_CANCELABLE = "pbar_horizontal_cancelable"; public static final String PROGRESS_DIALOG_STR_MESSAGE = "pbar_message"; public static final String PROGRESS_DIALOG_INT_MAX = "pbar_max_bar"; public static final String PROGRESS_DIALOG_INT_VALUE = "pbar_value"; protected static final int PROGRESS_DIALOG_INT_MAX_VALUE = 100; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.progress); progessDialog_g = new ProgressDialog(this);
Now we want to use Activity, so let's start by calling it:
final Intent i = new Intent(parentActivity, <packages>.ProgressActivity); i.putExtra(ProgressActivity.PROGRESS_DIALOG_BOOL_CANCELABLE, cancelable_g); i.putExtra(ProgressActivity.PROGRESS_DIALOG_BOOL_HORIZONTAL_BAR, showProgress_g); i.putExtra(ProgressActivity.PROGRESS_DIALOG_STR_MESSAGE, message_g); i.putExtra(ProgressActivity.PROGRESS_DIALOG_INT_MAX, ProgressActivity.PROGRESS_DIALOG_INT_MAX_VALUE); parentActivity.startActivity(i);
Thus, we launch the current ProgressActivity, which is waiting for various transfers. But first, we need an IntentService that sends broadcasts.
So release:
public class ExampleProgressService extends IntentService { protected PendingIntent pi_g = null; private static final String DEBUG_TAG = "ExampleProgressService"; public static final String PROGRESS_DIALOG_BROADCAST_INIT = "Dialog.Progress.Init"; public static final String PROGRESS_DIALOG_BROADCAST_FINISH = "Dialog.Progress.Finish"; public static final String PROGRESS_DIALOG_BROADCAST_UPDATE = "Dialog.Progress.Update"; public static final String PROGRESS_DATA_RESULT = "Result"; public static final String PROGRESS_DATA_RESULT_ERROR_MESSAGE = "Result.Error.Message"; public static final String PROGRESS_DATA_RESULT_ERROR_EXCEPTION = "Result.Error.Exception"; public static final String PROGRESS_DATA_RESULT_STATUS_BOOL = "Result.Status.boolean"; public static final String PROGRESS_DATA_PENDING_RESULT = "PendingResult"; public ExampleProgressService() { super("ExampleProgressService"); } private void closeProgressActivity() { Intent intent = new Intent(PROGRESS_DIALOG_BROADCAST_FINISH); sendBroadcast(intent); } private void extractVariablesFromIntentAndPrepare(Intent intent) throws Exception { pi_g = (PendingIntent) intent .getParcelableExtra(PROGRESS_DATA_PENDING_RESULT); if (pi_g == null) { throw new Exception("There is no pending intent!"); } private void failed(Exception e, String message) { Intent i = new Intent(); i.putExtra(PROGRESS_DATA_RESULT_ERROR_EXCEPTION, e); i.putExtra(PROGRESS_DATA_RESULT_ERROR_MESSAGE, message); send(i, false); } private void initProgressActivity() { Intent intent = new Intent(PROGRESS_DIALOG_BROADCAST_INIT); intent.putExtra(PROGRESS_DIALOG_BOOL_HORIZONTAL_BAR, multipart_g); sendBroadcast(intent); } @Override protected void onHandleIntent(Intent intent) { extractVariablesFromIntentAndPrepare(intent); initProgressActivity();
The result of the calculation must be available in parentActivity , so we create a PendingIntent in this Activity and call the IntentService.
// Some identifier for the call int requestCode = 12345; final Intent sI = new Intent(ExampleProgressService.PROGRESS_SERVICE_ACTION); // Callback sI.putExtra(ExampleProgressService.PROGRESS_DATA_PENDING_RESULT, parentActivity .createPendingResult(requestCode, null, PendingIntent.FLAG_CANCEL_CURRENT)); // Service start parentActivity.startService(sI);
To get the results, we must override the onActivityResult(int requestCode, int resultCode, Intent data) method onActivityResult(int requestCode, int resultCode, Intent data) .
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data){
It was magic, I hope this helps you.