-1

I have a Service with registered ContentObserver. When my ContentObserver detects changes it sets Service's boolean variable to true. I also have a Thread running in the service which sleeps for some time and wakes up to check that variable.

When it detects change it needs some time to process some other code and I need to show ProgressDialog during the delay. How can I do this?

0

5 Answers 5

2

You should use AsyncTask instead.

Here is the link to the library. It is fairly simple:

1) onPreExecute() = show ProgressDialog

2) doInBackground() = execute your code

3) onPostExecute() = dismiss ProgressDialog

DONE :-)

1
  • But the OP wanted to do this from a Service, so even in an AsyncTask, he wouldn't have an appropriate Context to run a ProgressDialog, right? Oct 8, 2013 at 17:24
2

The essence of your question is that you want your service to send a message of some kind to your UI (to show a loading dialog).

There are four (or more) ways of going about this:

  • Intents: have your service send an intent to your activity
  • AIDL
  • Using the service object itself (as singleton)
  • Having your activity be a broadcast receiver

These options may seem familiar: How to have Android Service communicate with Activity

You'll have to read up on those options and take your pick.

1

AsyncTask is a good alternative, but still if you decided to go with threads, then in order to show the ProgressDialog on UI you will need to call runOnUiThread() method of the activity.

Let suppose you want to display the ProgressDialog in the MainActivity. Inside your Thread from Service you should have something like this:

MainActivity.this.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        // Display ProgressDialog here
    }
});
1
  • But the OP was trying to do this from a Service, so MainActivity.this wouldn't work, right? Oct 8, 2013 at 17:23
1

Thanks everyone for answers. I solve the problem using these steps - broadcast Intent when my variable was changed - create BroadcastReceiver for the intent( in Activity ) - inside BroadcastReceiver's method onReceive call runOnUiThread for my activity

0

I know this is an old thread but I have exactly what you needed because I just implemented this from a thread here. Please read Rachit Mishra's answer further down the page talking about a ProgressBar:

Communication between Activity and Service

I have this in my service:

public void sendMessage(int state) {
    Message message = Message.obtain();
    switch (state) {
        case 1://SHOW:
            message.arg1 = 1;
            break;
        case 0:
            message.arg1 = 0;
            break;
    }
    try {
        messageHandler.send(message);
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

Call sendMessage() with 1 or 0 to show or dismiss the ProgressDialog within your service.

And this is in my Main Activity:

private ProgressDialog progress;

public class MessageHandler extends Handler {
    @Override
    public void handleMessage(Message message) {
        int state = message.arg1;
        switch (state) {
            case 0://HIDE
                progress.dismiss();
                break;
            case 1://SHOW
                progress = ProgressDialog.show(MainActivity.this, (getResources().getString(R.string.CONNECTING) + "..."), (getResources().getString(R.string.PLEASE_WAIT) + "!"));  //show a progress dialog
                break;
        }
    }
}

The ProgressDialog cannot be shown from the service, it must be called from the activity or fragment. I hope I added all the code you need and that it works well for your needs. To be honest I'm not sure how the message handler works but it works for me! The naming is probably not the best either lol. Sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.