通过 IntentService 显示对话框?
Displaying a Dialog via IntentService?
我有一个 IntentService,我试图在某些过程完成后显示带有消息的对话框。
我可以通过做来祝酒
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(ServiceName.this, message, Toast.LENGTH_LONG);
}
});
但我想改为显示一个对话框,因为它可以显示更多文本并且看起来更清晰,适合我要执行的操作。但是当我尝试时:
handler.post(new Runnable() {
@Override
public void run() {
new AlertDialog.Builder(ServiceName.this)
.setTitle("Title")
.setMessage(message)
.create().show();
}
});
它抛出一个错误:
java.lang.IllegalStateException: You need to use a Theme.AppCompat
theme (or descendant) with this activity.
我不想摆弄我的清单,因为这可能会影响应用程序中的其他所有内容,但是有什么办法可以解决这个问题吗?我能以某种方式将 AppCompatActivity 传递到 IntentService 中吗?我能以某种方式将 IntentService 与 AppCompat 关联起来吗?我有哪些选择?
编辑:尝试使用对话框 Activity 方法:
Intent intent = new Intent(ServiceName.this, ActivityWithDialogTheme.class);
intent.putExtra(ActivityWithDialogTheme.MESSAGE, message);
startActivity(intent);
Can I somehow pass an AppCompatActivity into the IntentService?
没有
Can I somehow associate the IntentService with AppCompat?
没有
What are my options?
创建一个 dialog-themed activity,然后从您的服务启动 activity。
或者,让服务 post 在事件总线上发送一条消息,说明程序已完成。如果您碰巧在前台有一个 activity,它可以拾取事件总线消息并显示对话框。如果您在前台没有 activity,您的服务可以查找并执行其他操作(例如,显示 Notification
),以免打扰用户。
我有一个 IntentService,我试图在某些过程完成后显示带有消息的对话框。
我可以通过做来祝酒
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(ServiceName.this, message, Toast.LENGTH_LONG);
}
});
但我想改为显示一个对话框,因为它可以显示更多文本并且看起来更清晰,适合我要执行的操作。但是当我尝试时:
handler.post(new Runnable() {
@Override
public void run() {
new AlertDialog.Builder(ServiceName.this)
.setTitle("Title")
.setMessage(message)
.create().show();
}
});
它抛出一个错误:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
我不想摆弄我的清单,因为这可能会影响应用程序中的其他所有内容,但是有什么办法可以解决这个问题吗?我能以某种方式将 AppCompatActivity 传递到 IntentService 中吗?我能以某种方式将 IntentService 与 AppCompat 关联起来吗?我有哪些选择?
编辑:尝试使用对话框 Activity 方法:
Intent intent = new Intent(ServiceName.this, ActivityWithDialogTheme.class);
intent.putExtra(ActivityWithDialogTheme.MESSAGE, message);
startActivity(intent);
Can I somehow pass an AppCompatActivity into the IntentService?
没有
Can I somehow associate the IntentService with AppCompat?
没有
What are my options?
创建一个 dialog-themed activity,然后从您的服务启动 activity。
或者,让服务 post 在事件总线上发送一条消息,说明程序已完成。如果您碰巧在前台有一个 activity,它可以拾取事件总线消息并显示对话框。如果您在前台没有 activity,您的服务可以查找并执行其他操作(例如,显示 Notification
),以免打扰用户。