gpt4 book ai didi

android - 从服务启动 AlertDialog.Builder

转载 作者:太空宇宙 更新时间:2023-11-03 11:37:00 25 4
gpt4 key购买 nike

我正在尝试通过使用 AlertDialog.Builder 从服务启动复选框对话框但我收到以下错误:

当我在没有 builder.getWindow().setType() 的情况下启动对话框时出现此错误:

05-28 10:48:42.816: E/AndroidRuntime(18510): FATAL EXCEPTION: main
05-28 10:48:42.816: E/AndroidRuntime(18510): Process: com.bustracker, PID: 18510
05-28 10:48:42.816: E/AndroidRuntime(18510): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
05-28 10:48:42.816: E/AndroidRuntime(18510): at android.view.ViewRootImpl.setView(ViewRootImpl.java:691)
05-28 10:48:42.816: E/AndroidRuntime(18510): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:288)
05-28 10:48:42.816: E/AndroidRuntime(18510): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
05-28 10:48:42.816: E/AndroidRuntime(18510): at android.app.Dialog.show(Dialog.java:312)
05-28 10:48:42.816: E/AndroidRuntime(18510): at android.app.AlertDialog$Builder.show(AlertDialog.java:991)
05-28 10:48:42.816: E/AndroidRuntime(18510): at com.bustracker.TrackingService.stop_popup(TrackingService.java:370)
05-28 10:48:42.816: E/AndroidRuntime(18510): at com.bustracker.TrackingService.onAsyncTaskFinished(TrackingService.java:305)

我试着用 builder.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 来启动它

但是我得到了这个错误:

The method getWindow() is undefined for the type

private void stop_popup(final ArrayList<Integer> routeList) {


int routeListSize = routeList.size();

if (routeListSize > 0) {

String[] charSequence = new String[routeList.size()];
for (int i = 0; i < routeList.size(); i++) {
charSequence[i] = String.valueOf(routeList.get(i));
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("Has this route arrived the stop? ");

builder.setMultiChoiceItems(charSequence, null,
new DialogInterface.OnMultiChoiceClickListener() {

@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {

if (isChecked) {

route_number = routeList.get(which);

}
}
});

builder.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {

}
});
builder.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {

}
});

builder.create();
builder.show();
}
}

最佳答案

如果你想在 Android Service 中弹出一个对话框,你有两种方法:

  1. Activity用作Dialog

  2. 使用AlertDialog.Builder,但您需要将对话框配置为系统警报通过使用 dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

示例代码如下:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Test dialog"));
builder.setIcon(R.drawable.icon);
builder.setMessage("Content");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Do something
dialog.dismiss();
});
builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alert.show();

另外,记得在你的 AndroidManifest.xml 中添加权限

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

关于android - 从服务启动 AlertDialog.Builder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30502191/

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