Skip to content Skip to sidebar Skip to footer

Show Progressdialog From Thread Inside The Service

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 wh

Solution 1:

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 :-)

Solution 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.

Solution 3:

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
    publicvoidrun() {
        // Display ProgressDialog here
    }
});

Solution 4:

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

Solution 5:

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:

publicvoidsendMessage(int state) {
    Messagemessage= Message.obtain();
    switch (state) {
        case1://SHOW:
            message.arg1 = 1;
            break;
        case0:
            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;

publicclassMessageHandlerextendsHandler {
    @Override
    publicvoidhandleMessage(Message message) {
        int state = message.arg1;
        switch (state) {
            case0://HIDE
                progress.dismiss();
                break;
            case1://SHOW
                progress = ProgressDialog.show(MainActivity.this, (getResources().getString(R.string.CONNECTING) + "..."), (getResources().getString(R.string.PLEASE_WAIT) + "!"));  //show a progress dialogbreak;
        }
    }
}

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.

Post a Comment for "Show Progressdialog From Thread Inside The Service"