Reputation: 2060
I have an IntentService where I am trying to display a Dialog with a message after some procedures have finished.
I can show a Toast by doing
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(ServiceName.this, message, Toast.LENGTH_LONG);
}
});
but I want to display a Dialog instead, since it can display more text and looks cleaner for what I am trying to do. But when I try:
handler.post(new Runnable() {
@Override
public void run() {
new AlertDialog.Builder(ServiceName.this)
.setTitle("Title")
.setMessage(message)
.create().show();
}
});
It throws an error:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
I don't want to go fiddling around with my Manifest because that might affect everything else in the app, but is there any way around this? Can I somehow pass an AppCompatActivity into the IntentService? Can I somehow associate the IntentService with AppCompat? What are my options?
Edit: Trying to use the Dialog Activity approach:
Intent intent = new Intent(ServiceName.this, ActivityWithDialogTheme.class);
intent.putExtra(ActivityWithDialogTheme.MESSAGE, message);
startActivity(intent);
Upvotes: 1
Views: 501
Reputation: 1006644
Can I somehow pass an AppCompatActivity into the IntentService?
No.
Can I somehow associate the IntentService with AppCompat?
No.
What are my options?
Create a dialog-themed activity, and start that activity from your service.
Or, have the service post a message on an event bus saying that the procedures have finished. If you happen to have an activity in the foreground, it can pick up the event bus message and display the dialog. If you do not have an activity in the foreground, your service can find and and do something else (e.g., show a Notification
), so as to not interrupt the user.
Upvotes: 1