如何避免WebView页面上的连续警报加载完成?Android

时间:2017-09-08 04:33:53

标签: javascript android webview alertdialog

我有一个Webview,因为我在webview页面加载了一些说明页面

这是我的示例代码

public void onPageFinished(WebView view, String url) {

if (!getSharedPreferences("MainA_SP", MODE_PRIVATE)
        .getBoolean("checkbox", false)) {

    AlertDialog.Builder adb=new AlertDialog.Builder(MainA.this);
    LayoutInflater adbInflater = LayoutInflater.from(MainA.this);
    View eulaLayout = adbInflater.inflate(R.layout.instpopup, null);
    chkbx = (CheckBox)eulaLayout.findViewById(R.id.skip);
    adb.setView(eulaLayout);
    adb.setTitle("Welcome To Sample Page");
    adb.setMessage(Html.fromHtml("App Instructions ....."));
    adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            String checkBoxResult = "NOT checked";
            if (chkbx.isChecked())  checkBoxResult = "checked";
            SharedPreferences settings = getSharedPreferences(MainA_SP, 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("skipMessage", checkBoxResult);
            // Commit the edits!
            editor.commit();
            return;
        } });

    adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            String checkBoxResult = "NOT checked";
            if (chkbx.isChecked())  checkBoxResult = "checked";
            SharedPreferences settings = getSharedPreferences(MainA_SP, 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("skipMessage", checkBoxResult);
            // Commit the edits!
            editor.commit();
            return;
        } });
    SharedPreferences settings = getSharedPreferences(MainA_SP, 0);
    String skipMessage = settings.getString("skipMessage", "NOT checked");
    if (!skipMessage.equalsIgnoreCase("checked") ) adb.show();
}

try{
    if (progressBar.isShowing()) {
        progressBar.dismiss();
      .
      .
      .
      .

    }
}catch(Exception exception){
    exception.printStackTrace();
}
}

所以这里它与Checkbox和sharedprefs一起工作正常

但问题是我已经在页面加载完成时给出了此警报 因此,我为单个网址单页获取多次警报

我需要点击警报每次打开应用程序...如果我勾选Checkbox它没有显示但是对于第一次运行警报显示多次

更新

我想在页面上显示警报已成功加载

1 个答案:

答案 0 :(得分:0)

如果你想在完成后立即显示警告,你可以使用布尔值来检查它是否可见,如下例所示。

 private boolean alertVisiblity = false;

onPageFinished(){

//show your alert here
if(!alertVisiblity){
 alertVisiblity = true;
new AlertDialog.Builder(MainActivity.this)
                .setCancelable(false)
                .setTitle("Alert")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                        alertVisiblity = false; //or if you want to show it once never make it false or you can make it false before next call
                    }
                }).show();


}