无法解除AlertDialog

时间:2014-02-20 16:07:25

标签: android

您需要从服务中显示AlertDialog。

这是我的实施。

我添加了许可:

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

但是我无法解除对话!

我单击“确定”按钮,但ID不会消失!!!

所有系统都冻结了该对话框!!!

请帮助!!!

public class CreditcheckService extends IntentService {
AlertDialog dialog;
public CreditcheckService() {
    super("CreditcheckService");
}

@Override
protected void onHandleIntent(Intent intent) {
    AlertDialog.Builder builder = new AlertDialog.Builder(
            getApplicationContext());

    builder.setMessage(R.string.please_connect).setTitle(
            R.string.unable_to);
    builder.setPositiveButton(R.string.okay_action,
            new OnClickListener() {
        @Override
        public void onClick(DialogInterface arg,
                int which) {dialog.cancel();
        }
    });
    dialog= builder.create(); 
    dialog.getWindow().setType(
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    dialog.show();
}

2 个答案:

答案 0 :(得分:4)

这适用于我的服务

              try{
                    new Thread() {
                        @Override
                        public void run() {
                            Looper.prepare();
                            AlertDialog.Builder builder = new AlertDialog.Builder(
                                    getApplicationContext());

                            builder.setMessage("My messgae").setTitle("My Title");
                            builder.setPositiveButton("Ok",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface arg,
                                                            int which) {dialog.cancel();
                                        }
                                    });

                            dialog= builder.create();
                            dialog.getWindow().setType(
                                    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                            dialog.show();
                            Looper.loop();
                        }
                    }.start();
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }

答案 1 :(得分:0)

试试这个:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("")
            .setCancelable(false)
            .setPositiveButton("Yes",null);
    AlertDialog alert = builder.create();
    alert.show();   
}