Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6075827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:32:17+00:00 2026-05-23T10:32:17+00:00

I have a method public void writeEntry(Activity ctx, Entry entry) which get some data

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:32:17+00:00Added an answer on May 23, 2026 at 10:32 am

    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 …

    <?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 the related code:

    public class ProgressActivity extends Activity {
        /**
         * ProgressDialog which is shown
         */
        private ProgressDialog progessDialog_g;
    
        /**
         * Instance of the BroadcastReceiver
         */
        private BroadcastReceiver receiver_g;
    
        /**
         * Identifier for the different settings of the ProgressDialog
         */
        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);
    
            // Reads and sets the settings for the ProgressDialog
            Intent i = getIntent();
            progessDialog_g.setCancelable(i.getBooleanExtra(
                    PROGRESS_DIALOG_BOOL_CANCELABLE, false));
            if (i.getBooleanExtra(
                    PROGRESS_DIALOG_BOOL_HORIZONTAL_BAR, false)) {
                progessDialog_g.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            } else {
                progessDialog_g.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            }
            progessDialog_g
                    .setMessage(i
                            .getStringExtra(PROGRESS_DIALOG_STR_MESSAGE));
            progessDialog_g.setMax(i.getIntExtra(
                    PROGRESS_DIALOG_INT_MAX, 100));
    
            // Create the IntentFilter for the different broadcast messages
            IntentFilter iFilter =
                    new IntentFilter(
                            ExampleProgressService.PROGRESS_DIALOG_BROADCAST_INIT);
            iFilter.addAction(ExampleProgressService.PROGRESS_DIALOG_BROADCAST_UPDATE);
            iFilter.addAction(ExampleProgressService.PROGRESS_DIALOG_BROADCAST_FINISH);
    
            // Creates the BroadcastReceiver
            receiver_g = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent){
                    Log.d(DefaultPreferences.DEBUG_PREFIX + "ProgressActivity",
                            intent.getAction());
    
                    if (ExampleProgressService.PROGRESS_DIALOG_BROADCAST_INIT
                            .equals(intent.getAction())) {
                        // Sets the ProgressDialog style
                        if (intent
                                .getBooleanExtra(
                                        PROGRESS_DIALOG_BOOL_HORIZONTAL_BAR,
                                        false)) {
                            progessDialog_g
                                    .setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                        } else {
                            progessDialog_g
                                    .setProgressStyle(ProgressDialog.STYLE_SPINNER);
                        }
    
                        // Shows the ProgressDialog    
                        progessDialog_g.show();
                    } else if (ExampleProgressService.PROGRESS_DIALOG_BROADCAST_UPDATE
                            .equals(intent.getAction())) {
                        // Updates the ProgressDialog
                        int value =
                                intent.getIntExtra(
                                        PROGRESS_DIALOG_INT_VALUE,
                                        -1);
                        if (value != -1) {
                            progessDialog_g.setProgress(value);
                        }
                    } else if (ExampleProgressService.PROGRESS_DIALOG_BROADCAST_FINISH
                            .equals(intent.getAction())) {
                        // Finishs the ProgressDialog
                        progessDialog_g.cancel();
                        finish();
                    }
                }
            };
    
            // Registers the BroadcastReceiver
            registerReceiver(receiver_g, iFilter);
        }
    
        @Override
        protected void onDestroy(){
            unregisterReceiver(receiver_g);
            super.onDestroy();
        }
    }
    

    Now we want to use the Activity, so lets start with 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);
    

    So we habe a running ProgressActivity, which waits for different broadcasts. But first we need the IntentService, which sends the broadcasts.
    So lets go:

    public class ExampleProgressService extends IntentService {
        /**
         * PendingIntent for callback.
         */
        protected PendingIntent pi_g = null;
    
        private static final String DEBUG_TAG = "ExampleProgressService";
    
        /**
         * Message identifier for ProgressDialog init
         */
        public static final String PROGRESS_DIALOG_BROADCAST_INIT = "Dialog.Progress.Init";
        /**
         * Message identifier for ProgressDialog finish
         */
        public static final String PROGRESS_DIALOG_BROADCAST_FINISH = "Dialog.Progress.Finish";
        /**
         * Message identifier for ProgressDialog update
         */
        public static final String PROGRESS_DIALOG_BROADCAST_UPDATE = "Dialog.Progress.Update";
    
        /**
         * Identifier of the result for intent content
         */
        public static final String PROGRESS_DATA_RESULT = "Result";
        /**
         * Identifier of the result error for intent content
         */
        public static final String PROGRESS_DATA_RESULT_ERROR_MESSAGE = "Result.Error.Message";
        /**
         * Identifier of the result error exception for intent content
         */
        public static final String PROGRESS_DATA_RESULT_ERROR_EXCEPTION = "Result.Error.Exception";
        /**
         * Identifier of the result status for intent content
         */
        public static final String PROGRESS_DATA_RESULT_STATUS_BOOL = "Result.Status.boolean";
    
        /**
         * Identifier of the pending intent for intent content
         */
        public static final String PROGRESS_DATA_PENDING_RESULT = "PendingResult";
    
        public ExampleProgressService() {
            super("ExampleProgressService");
        }
    
        /**
         * Send the finish message.
         */
        private void closeProgressActivity() {
            Intent intent = new Intent(PROGRESS_DIALOG_BROADCAST_FINISH);
    
            sendBroadcast(intent);
        }
    
        /**
         * Do some magic with the intent content
         */
        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!");
        }
    
        /**
         * Sends an error message.
         */
        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);
        }
    
        /**
         * Sends the init message.
         */
        private void initProgressActivity() {
            Intent intent = new Intent(PROGRESS_DIALOG_BROADCAST_INIT);
    
            intent.putExtra(PROGRESS_DIALOG_BOOL_HORIZONTAL_BAR,
                    multipart_g);
    
            sendBroadcast(intent);
        }
    
        /**
         * (non-Javadoc)
         * 
         * @see android.app.IntentService#onHandleIntent(android.content.Intent)
         */
        @Override
        protected void onHandleIntent(Intent intent) {
            extractVariablesFromIntentAndPrepare(intent);
    
            initProgressActivity();
    
            // do your calculation here and implements following code
            Intent intent = new Intent(PROGRESS_DIALOG_BROADCAST_UPDATE);
    
            intent.putExtra(PROGRESS_DIALOG_INT_VALUE, progressValue);
    
            sendBroadcast(intent);
    
            // If you finished, use one of the two methods to send the result or an error
            success(result);
            failed(exception, optionalMessage);
        }
    
        /**
         * Sends the data to the calling Activity
         */
        private void send(Intent resultData, boolean status) {
            resultData.putExtra(PROGRESS_DATA_RESULT_STATUS_BOOL, status);
    
            closeProgressActivity();
    
            try {
                pi_g.send(this, Activity.RESULT_OK, resultData);
            } catch (PendingIntent.CanceledException e) {
                Log.e(DEBUG_TAG,
                        "There is something wrong with the pending intent", e);
            }
        }
    
        /**
         * Sends the result message.
         */
        private void success(String result) {
            Intent i = new Intent();
            i.putExtra(PROGRESS_DATA_RESULT, result);
    
            send(i, true);
        }
    }
    

    The result of the calculation progress shall be available in the parentActivity, so we create the PendingIntent in that 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);
    

    For receiving the results, we have to override the method onActivityResult(int requestCode, int resultCode, Intent data).

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        // Compares the requestCode with the requestCode from above
        if (requestCode == ...) {
            if (data.getBooleanExtra(ExampleProgressService.PROGRESS_DATA_RESULT_STATUS_BOOL, false)) {
                // Calculation was success
                data.getStringExtra(ExampleProgressService.PROGRESS_DATA_RESULT);
            } else
            {
                // Calculation is failed
                data.getStringExtra(ExampleProgressService.PROGRESS_DATA_RESULT_ERROR_MESSAGE);
                ((Exception) data.getSerializableExtra(ExampleProgressService.PROGRESS_DATA_RESULT_ERROR_EXCEPTION));
            }
        }
    }
    

    That was the magic, I hope it will help you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interface method public void Execute(ICommand command); which needs to pass known
I have this method public void SetSource(Stream streamSource); which sets the source of BitmapSource
If we have a method: public void doSomething(){ synchronized(this){ //some code processing here }
Suppose you have a method: public void Save(Entity data) { this.repositoryIocInstance.EntitySave(data); } Would you
I have a method public void MyMethod(decimal val) { } I want to call
If I have the method public void send() { /* some code */ }
I have the following method: public void PutFile(string ID, Stream content) { try {
I have the following method public static void SerializeToXMLFile(Object obj,Type type, string fileName) {
If I have a method such as: public void MyMethod(int arg1, string arg2) How
If I have a method like this: public void DoSomething(int Count, string[] Lines) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.