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

  • SEARCH
  • Home
  • 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 3406952
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:41:28+00:00 2026-05-18T05:41:28+00:00

This is my fist stackoverflow post so please be gentle with me! I’m sure

  • 0

This is my fist stackoverflow post so please be gentle with me! I’m sure that what i’m trying to do is possible and it is something I have done (or not done?) that is causing the problem… I’m just not sure what that something is.

What i’m trying to do:
Show a ProgressDialog while my app synchronises and processes the downloaded data.

The problem:
The ProgressDialog shows but doesn’t spin (which makes it look like it has frozen), the synchronisation and processing happens, the ProgressDialog closes, the app continues as normal.

How I am currently tring to do that:
Create a ProgressDialog – in my Activity
Do the synchronisation – in my Service
Process the data – in my Service
Dismis the ProgressDialog – in my Activity

The things I have tried:
Using a Thread (code below)
Using a AsynTask (can provide code if needed)
Using a Handler (can provide code if needed)

After spending lots of time searching for the answer to my question it seems others have had the same or similar problem and managed to solve it using one of the ideas above. However I have been unable to implement any of these ideas to solve my particular issue. This makes me sure that it is something I am doing wrong… I’m just not sure what.

The original code I wrote and am using as a basis for all my attempted fixes:
First

public class myActivity extends ListActivity {
    private ProgressDialog dialog;
    private boolean mIsBound;
    private myService mBoundService;
    private ServiceConnection mConnection;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*if we need to login start login activity for result*/
        ...
    }

    ...
    /*other methods*/
    ...
}

then

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    switch (requestCode){
        case ACTIVITY_LOGIN:
            /*after login we know we always need to sync*/
         dialog = new ProgressDialog(myActivity.this);
         dialog.setMessage("Synchronising...");
         dialog.setIndeterminate(true);
         dialog.setCancelable(false);
         dialog.show();

            Thread doBind = new Thread(new Runnable(){public void run(){doBindService();}});
            doBind.start();
            break;
    }
}

so now i am assuming that doBind is happening in a different thread leaving the UI thread with nothing to do other than show the ProgressDialogue…?

private boolean doBindService() {
 mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mBoundService = ((myService.LocalBinder)service).getService();      
            while (mBoundService.isRunning()){
                /*wait for service to finish before doing anything else... myMethod() is expecting the sync to have completed*/
                /*I suspect this loop to be the thing causing the problem. But I'm not sure why because it is in a different thread so shouldn't interfear with the dialog?! And if that is what is causing the problem then how else can I do this?*/
            }
            /*get the activity to do Stuff with the downloaded data*/
            myMethod();
            /*finished with the service for now so unbind*/
            doUnbindService();
            if (dialog != null) {
                dialog.dismiss();
            }
        }

        public void onServiceDisconnected(ComponentName className) {
            mBoundService = null;
        }
    };

    boolean myReturn = bindService(new Intent(myActivity.this, myService.class), mConnection, Context.BIND_AUTO_CREATE);
    mIsBound = myReturn;
 return myReturn;
}

private void doUnbindService() {
    if (mIsBound) {
        unbindService(mConnection);
        mIsBound = false;
    }
}

Please let me know if you require any further information in order to assist me with this issue.


EDIT:

Here is the code I am using to use a handler instead. This displays the same behavior (spinner doesn’t spin) as before.

Thread doBind = new Thread(new Runnable(){
    public void run(){
        doBindService();
    }
});
doBind.start();

.

private Handler handler = new Handler() {
    @Override
public void handleMessage(Message msg) {
    dialog.dismiss();
}
};

.

private boolean doBindService() {
 mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mBoundService = ((myService.LocalBinder)service).getService();      
            while (mBoundService.isRunning()){
            }
            //...do stuff same as before...
            handler.sendEmptyMessage(0);
        }

        public void onServiceDisconnected(ComponentName className) {
            mBoundService = null;
        }
    };

    boolean myReturn = bindService(new Intent(myActivity.this, myService.class), mConnection, Context.BIND_AUTO_CREATE);
    mIsBound = myReturn;
 return myReturn;
}
  • 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-18T05:41:29+00:00Added an answer on May 18, 2026 at 5:41 am

    The problem with the spinner is related to the ServiceConnection which you create in the new thread. unfortunately for you, this will be run on your main thread / gui thread.

    From the API reference: (http://developer.android.com/reference/android/content/ServiceConnection.html)

    Like many callbacks from the system,
    the methods on this class are called
    from the main thread of your process.

    So, since I’m not familiar with services I can only guess that one way of communicating when your service is done downloading data would be to broadcast an Intent which you listen for in your Activity, and then stop the ProgressDialog. But there are certainly other ways as well.

    And as a side note, you should never modify the gui from any thread other then the main thread, which you’re trying to do by spawning a new Thread which tries to dismiss the dialog. You should only dismiss the dialog from the main thread.

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

Sidebar

Related Questions

It's not the fist time that i stuck to this problem. Everytime when i
(this is my fist post) I have written a PHP-script to enter a data-set
Fist off, been lurking for years, hopefully this post will be helpful to more
Hi I have this program that I am writing that uses the struct below.
I have this Ir image that has captured the veins under the skin. I
I have strings in my python application that look this way: test1/test2/foo/ Everytime I
Fist all...i am not sure PROGRESS BAR is the appropriate word to describe what
Fist of all I want to show you my code/storyboard: This is my storyboard:
This should be a simple one: I have an observableArray object called To in
Let me establish that this is part of a class assignment, so I'm definitely

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.