gpt4 book ai didi

Intent 服务中的 Android 警报对话框

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:51:16 27 4
gpt4 key购买 nike

我想在 Intent 服务中显示一个警告对话框。

   AlertDialog alertDialog = new AlertDialog.Builder(this).create();

这会抛出以下异常

   Unable to add window — token null is not for an application

我也试过 IntentService.this 和 getApplicationContext() 。在我不想使用 Activity 之间。我只想显示一个带有少量文本的简单警报对话框。

最佳答案

需要 Activity 来显示 AlertDialog,因为我们无法从任何 Service 显示 Dialog

解决方案。

创建 Activity 作为对话框主题并从 Service 启动该 Activity

只需要像下面这样在menifest.xml中注册你的Activity

android:theme="@android:style/Theme.Dialog"

android:theme="@android:style/Theme.Translucent.NoTitleBar"

MyDialog.java

public class MyDialog extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("your title");
alertDialog.setMessage("your message");
alertDialog.setIcon(R.drawable.icon);

alertDialog.show();
}
}

关于 Intent 服务中的 Android 警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22627184/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com