问题
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?
回答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 :-)
回答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.
回答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
public void run() {
// Display ProgressDialog here
}
});
回答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
回答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:
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.
来源:https://stackoverflow.com/questions/11797533/show-progressdialog-from-thread-inside-the-service