I have a method public void writeEntry(Activity ctx, Entry entry) which get some data and have to call a native method, which takes longer to finish.
So I created an AsyncTask which handles the ProgressDialog and the native method. It works great in an own Activity to test it, in that Activity I used a callback interface and so on.
In my case I have the above described method and have to execute the AsyncTask. The executing can’t be in that method because it doesn’t halt the further execution.
I need the result from the native method before I can continue with the execution.
Is there any possibility to wait for the AsyncTask til it is finished? The method wait() isn’t an option because the UI Thread will wait, too and so the sense of a ProgressDialog will be lost.
Can I use the method runOnUiThread() from the given parameters or is the only solution to start an own Activity?
My first solution was to use callback methods with an interface implementation see the example https://stackoverflow.com/a/6396376/390177.
After chatting a while in the Android chat I heard that there is a more praticable solution.
You can use of an IntentService in combination with a PendingIntent.
The communication is realized with Intent‘s.
If you want to use a ProgressDialog, you need an own Activity for it, which register for example a BroadcastReciever and the IntentService sends it actual status per Broadcast.
But lets start now.
First we create the Activity, which contains the ProgressDialog and a registered BroadcastReceiver. The BroadcastReceiver listen for messages about updating and finishing the dialog.
For the Activity we needs the layout …
… and the related code:
Now we want to use the Activity, so lets start with calling it:
So we habe a running ProgressActivity, which waits for different broadcasts. But first we need the IntentService, which sends the broadcasts.
So lets go:
The result of the calculation progress shall be available in the parentActivity, so we create the PendingIntent in that Activity and call the IntentService.
For receiving the results, we have to override the method
onActivityResult(int requestCode, int resultCode, Intent data).That was the magic, I hope it will help you.