问题
Basically, i have an activity that has a progress dialog, i'm sending a message to an Intent to load all the data from the internet without any hiccup in the application. However, i was able to send a message to the service but i wasn't able to resend the message to the activity. What to do?
Here's how i send message to the service:
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
}
};
final Intent intent = new Intent(this, FillingDatabase.class);
final Messenger messenger = new Messenger(handler);
intent.putExtra("messenger", messenger);
startService(intent);
And here's how i recieve the message from the service:
messenger = (Messenger) intent.getParcelableExtra("messenger");
message = Message.obtain(null, 1234);
messenger.send(message);
But i wasnt able to know how to recieve that message in an activity, can someone explain the approach? Thank you!
回答1:
Try out as below:
private Handler handler = new Handler()
{
public void handleMessage(Message message)
{
final Intent intent = new Intent(this, FillingDatabase.class);
final Messenger messenger = new Messenger(handler);
intent.putExtra("messenger", messenger);
startService(intent);
};
};
回答2:
use
handler.dispatchMessage();
and send an empty message.declare handler static and access it by class name or simply create object of class and then access it.
i think you can also use
handler.SendEmptyMessage();
回答3:
Here comes your exact solution,
Issue was already solved here sending message from IntentService to Activity
And for more better understanding about this, Here comes an working example where you can directly use it for implementation Go here
Mark as OK if it works for you.
来源:https://stackoverflow.com/questions/20796612/how-to-send-a-message-from-an-intent-service-to-an-activity