gpt4 book ai didi

android - 错误从服务启动 AlertDialog.Builder

转载 作者:行者123 更新时间:2023-11-30 00:21:05 31 4
gpt4 key购买 nike

大家好我正在尝试在收到推送通知时从服务启动 CountDownTimer 对话框。我想要唤醒屏幕并使用系统警报对话框; 我使用权限:

"android.permission.SYSTEM_ALERT_WINDOW", "android.permission.WAKE_LOCK"

但是当收到通知时,出现以下错误和服务错误:

                                                                    Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)

at android.support.v7.app.AppCompatDialog.<init>(AppCompatDialog.java:46)
at android.support.v7.app.AlertDialog.<init>(AlertDialog.java:97)
at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:929)
at com.iransnaptaxi.taxi.Server.PusheServic.ShowDialog(PusheServic.java:91)
at com.iransnaptaxi.taxi.Server.PusheServic.onMessageReceived(PusheServic.java:48)

at android.os.AsyncTask$2.call(AsyncTask.java:297)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 

我试着用下面的鳕鱼来烤它:

private void ShowDialog(){

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "taxiWakeLock");
wakeLock.acquire();



final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getApplicationContext(), R.style.AppTheme_MaterialDialogTheme);

dialogBuilder.setTitle("درخواست سفر")
.setMessage("درخواست سفر دارید").setNegativeButton("لغو", null)
.setPositiveButton("مشاهد سفر", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {



}
});

final AlertDialog dialog = dialogBuilder.create();
final Window dialogWindow = dialog.getWindow();
final WindowManager.LayoutParams dialogWindowAttributes = dialogWindow.getAttributes();


dialogWindow.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialogWindow.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
dialogWindowAttributes.windowAnimations = R.anim.dialogue_scale_anim_open;
dialog.show();
new CountDownTimer(15000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
dialog.setMessage("زمان مانده تا لغو درخواست"+"00:"+ (millisUntilFinished/1000));
}

@Override
public void onFinish() {
dialog.dismiss();
//info.setVisibility(View.GONE);
}
}.start();



}

最佳答案

似乎您正在尝试显示来自AsyncTaskdoInBackground() 方法的对话框。从 onPostExecute() 方法显示它。 UI 元素只能从 UI 线程使用,不能从后台线程使用。

如果出现 Looper 错误,试试这个:

Looper.prepare();
showDialog();
Looper.loop();

您还需要 Activity 上下文来创建 AlertDialogs。查看答案here从服务内部创建对话框。

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

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