2017-04-15 50 views
1

我有一個Web視圖在這一點擊與幫助JavaScript界面​​它將打開我的android活動從web視圖在這裏其工作正常...Android Web Alert中的「不要再顯示」複選框

所以這裏當用戶點擊任何按鈕,這是JS-接口..我已經加入就可以了警報

使以往任何時候,當用戶點擊......它會顯示一個對話框......

現在我跟着this添加一個選項就像不要再問....複選框

在這裏,我已經給共享首選項和複選框...但它不工作...

和更多的事情,複選框是可行的網絡視圖...它應該只顯示警報..但其顯示所有的時間...

任何一個可以建議我..如何顯示與複選框警報..不要在Android的web視圖再問......記住複選框...

java.lang.RuntimeException: Unable to start activity ComponentInfo{Mypackage/myactivity}: android.view.InflateException: Binary XML file line #18: Error inflating class checkbox 

更新

這裏是my code爲我添加了example code ...在警報...

其實我需要一個複選框沒有XML複選框...(由於複選框的XML我的主Web視圖獲取一些錯誤) 是否有可能..如果是讓我知道...

回答

2

我要你的問題分解成兩個部分

1)顯示對話框,並記住,如果用戶選擇了不再顯示它

具有共享首選項的簡單警報對話框ca ñ那樣做。

private void handleAlertDialog() { 
    //Handle showAlert 
    //check if allow to show dialog 
    if (!getSharedPreferences("MyPrefsFile1", MODE_PRIVATE) 
      .getBoolean("ShowDialog", false)) { 

     final CharSequence[] items = {"Would like to Remember this setting ? "}; 

     AlertDialog dialog = new AlertDialog.Builder(WebViewActivity.this) 
       .setTitle("Allow location access") 
       .setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) { 
         if (isChecked) { 
          //If the user checked the item, add it to the selected items 
          // Don't Allow, remember 


          getSharedPreferences("MyPrefsFile1", MODE_PRIVATE) 
            .edit() 
            .putBoolean("ShowDialog", true).commit(); 

          Toast.makeText(getApplicationContext(), "Remember", Toast.LENGTH_SHORT).show(); 
         } else { 


          getSharedPreferences("MyPrefsFile1", MODE_PRIVATE) 
            .edit() 
            .putBoolean("ShowDialog", false).commit(); 

          Toast.makeText(getApplicationContext(), "Don't Remember", Toast.LENGTH_SHORT).show(); 
         } 
        } 
       }).setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int id) { 
         // Your code when user clicked on OK 
         // You can write the code to save the selected item here 
        } 
       }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int id) { 
         // Your code when user clicked on Cancel 
        } 
       }).create(); 
     dialog.show(); 
    } 
} 

2)觸發從JavaScript此方法。

shouldOverrideUrlLoading(WebView view, String url)在Android N顯示您需要覆蓋shouldOverrideUrlLoading(WebView view, WebResourceRequest request)以及不推薦使用。

完整的WebViewClient類將如下所示。

/** 
    * WebViewClient subclass loads all hyperlinks in the existing WebView 
    */ 
    public class GeoWebViewClient extends WebViewClient { 

     Dialog loadingDialog = new Dialog(WebViewActivity.this); 

     @SuppressWarnings("deprecation") 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 

      if (url.startsWith("mailto:")) { 
       //Handle mail Urls 
       startActivity(new Intent(Intent.ACTION_SENDTO, Uri.parse(url))); 

      } else if (url.startsWith("tel:")) { 

       //Handle telephony Urls 
       startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url))); 

      } else if (url.startsWith("showalert:")) { 
       handleAlertDialog(); 

      } else { 
       view.loadUrl(url); 
      } 
      return true; 
     } 

     @TargetApi(Build.VERSION_CODES.N) 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { 
      final Uri uri = request.getUrl(); 
      if (uri.toString().startsWith("mailto:")) { 

       //Handle mail Urls 
       startActivity(new Intent(Intent.ACTION_SENDTO, uri)); 
      } else if (uri.toString().startsWith("tel:")) { 

       //Handle telephony Urls 
       startActivity(new Intent(Intent.ACTION_DIAL, uri)); 
      } else if (uri.toString().startsWith("showalert:")) { 

       //Handle Alert 
       handleAlertDialog(); 

      } else { 
       //Handle Web Urls 
       view.loadUrl(uri.toString()); 
      } 
      return true; 
     } 

     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      super.onPageStarted(view, url, favicon); 
      webViewPreviousState = PAGE_STARTED; 

      if (loadingDialog == null || !loadingDialog.isShowing()) 
       loadingDialog = ProgressDialog.show(WebViewActivity.this, "", 
         "Loading Please Wait", true, true, 
         new DialogInterface.OnCancelListener() { 

          @Override 
          public void onCancel(DialogInterface dialog) { 
           // do something 
          } 
         }); 

      loadingDialog.setCancelable(false); 
     } 


     @RequiresApi(api = Build.VERSION_CODES.M) 
     @Override 
     public void onReceivedError(WebView view, WebResourceRequest request, 
            WebResourceError error) { 


      if (isConnected()) { 
       final Snackbar snackBar = Snackbar.make(rootView, "onReceivedError : " + error.getDescription(), Snackbar.LENGTH_INDEFINITE); 
       snackBar.setAction("Reload", new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         webView.loadUrl("javascript:window.location.reload(true)"); 
        } 
       }); 
       snackBar.show(); 
      } else { 
       final Snackbar snackBar = Snackbar.make(rootView, "No Internet Connection ", Snackbar.LENGTH_INDEFINITE); 
       snackBar.setAction("Enable Data", new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         startActivityForResult(new Intent(Settings.ACTION_WIRELESS_SETTINGS), 0); 
         webView.loadUrl("javascript:window.location.reload(true)"); 
         snackBar.dismiss(); 
        } 
       }); 
       snackBar.show(); 
      } 

      super.onReceivedError(view, request, error); 

     } 

     @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 
     @Override 
     public void onReceivedHttpError(WebView view, 
             WebResourceRequest request, WebResourceResponse errorResponse) { 

      if (isConnected()) { 
       final Snackbar snackBar = Snackbar.make(rootView, "HttpError : " + errorResponse.getReasonPhrase(), Snackbar.LENGTH_INDEFINITE); 

       snackBar.setAction("Reload", new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         webView.loadUrl("javascript:window.location.reload(true)"); 
        } 
       }); 
       snackBar.show(); 
      } else { 
       final Snackbar snackBar = Snackbar.make(rootView, "No Internet Connection ", Snackbar.LENGTH_INDEFINITE); 
       snackBar.setAction("Enable Data", new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         startActivityForResult(new Intent(Settings.ACTION_WIRELESS_SETTINGS), 0); 
         webView.loadUrl("javascript:window.location.reload(true)"); 
         snackBar.dismiss(); 
        } 
       }); 
       snackBar.show(); 
      } 
      super.onReceivedHttpError(view, request, errorResponse); 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 

      if (webViewPreviousState == PAGE_STARTED) { 

       if (null != loadingDialog) { 
        loadingDialog.dismiss(); 
        loadingDialog = null; 
       } 
      } 
     } 
    } 

isConnected方法用於檢查網絡連接

/** 
* Check if there is any connectivity 
* 
* @return is Device Connected 
*/ 
public boolean isConnected() { 

    ConnectivityManager cm = (ConnectivityManager) 
      this.getSystemService(Context.CONNECTIVITY_SERVICE); 

    if (null != cm) { 
     NetworkInfo info = cm.getActiveNetworkInfo(); 
     return (info != null && info.isConnected()); 
    } 

    return false; 

} 

正如你可以看到我已經添加了一個過濾器來檢查URL開始showalert:。如果我的Web客戶端類捕獲以showalert開頭的任何URL,則會觸發handleAlert方法。

結果

enter image description here

2)觸發從JavaScript此方法。

shouldOverrideUrlLoading(WebView view, String url)在Android N顯示您需要覆蓋shouldOverrideUrlLoading(WebView view, WebResourceRequest request)以及不推薦使用。

完整的WebViewClient類將如下所示。

/** 
    * WebViewClient subclass loads all hyperlinks in the existing WebView 
    */ 
    public class GeoWebViewClient extends WebViewClient { 

     Dialog loadingDialog = new Dialog(WebViewActivity.this); 

     @SuppressWarnings("deprecation") 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 

      if (url.startsWith("mailto:")) { 
       //Handle mail Urls 
       startActivity(new Intent(Intent.ACTION_SENDTO, Uri.parse(url))); 

      } else if (url.startsWith("tel:")) { 

       //Handle telephony Urls 
       startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url))); 

      } else if (url.startsWith("showalert:")) { 
       handleAlertDialog(); 

      } else { 
       view.loadUrl(url); 
      } 
      return true; 
     } 

     @TargetApi(Build.VERSION_CODES.N) 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { 
      final Uri uri = request.getUrl(); 
      if (uri.toString().startsWith("mailto:")) { 

       //Handle mail Urls 
       startActivity(new Intent(Intent.ACTION_SENDTO, uri)); 
      } else if (uri.toString().startsWith("tel:")) { 

       //Handle telephony Urls 
       startActivity(new Intent(Intent.ACTION_DIAL, uri)); 
      } else if (uri.toString().startsWith("showalert:")) { 

       //Handle Alert 
       handleAlertDialog(); 

      } else { 
       //Handle Web Urls 
       view.loadUrl(uri.toString()); 
      } 
      return true; 
     } 

     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      super.onPageStarted(view, url, favicon); 
      webViewPreviousState = PAGE_STARTED; 

      if (loadingDialog == null || !loadingDialog.isShowing()) 
       loadingDialog = ProgressDialog.show(WebViewActivity.this, "", 
         "Loading Please Wait", true, true, 
         new DialogInterface.OnCancelListener() { 

          @Override 
          public void onCancel(DialogInterface dialog) { 
           // do something 
          } 
         }); 

      loadingDialog.setCancelable(false); 
     } 


     @RequiresApi(api = Build.VERSION_CODES.M) 
     @Override 
     public void onReceivedError(WebView view, WebResourceRequest request, 
            WebResourceError error) { 


      if (isConnected()) { 
       final Snackbar snackBar = Snackbar.make(rootView, "onReceivedError : " + error.getDescription(), Snackbar.LENGTH_INDEFINITE); 
       snackBar.setAction("Reload", new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         webView.loadUrl("javascript:window.location.reload(true)"); 
        } 
       }); 
       snackBar.show(); 
      } else { 
       final Snackbar snackBar = Snackbar.make(rootView, "No Internet Connection ", Snackbar.LENGTH_INDEFINITE); 
       snackBar.setAction("Enable Data", new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         startActivityForResult(new Intent(Settings.ACTION_WIRELESS_SETTINGS), 0); 
         webView.loadUrl("javascript:window.location.reload(true)"); 
         snackBar.dismiss(); 
        } 
       }); 
       snackBar.show(); 
      } 

      super.onReceivedError(view, request, error); 

     } 

     @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 
     @Override 
     public void onReceivedHttpError(WebView view, 
             WebResourceRequest request, WebResourceResponse errorResponse) { 

      if (isConnected()) { 
       final Snackbar snackBar = Snackbar.make(rootView, "HttpError : " + errorResponse.getReasonPhrase(), Snackbar.LENGTH_INDEFINITE); 

       snackBar.setAction("Reload", new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         webView.loadUrl("javascript:window.location.reload(true)"); 
        } 
       }); 
       snackBar.show(); 
      } else { 
       final Snackbar snackBar = Snackbar.make(rootView, "No Internet Connection ", Snackbar.LENGTH_INDEFINITE); 
       snackBar.setAction("Enable Data", new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         startActivityForResult(new Intent(Settings.ACTION_WIRELESS_SETTINGS), 0); 
         webView.loadUrl("javascript:window.location.reload(true)"); 
         snackBar.dismiss(); 
        } 
       }); 
       snackBar.show(); 
      } 
      super.onReceivedHttpError(view, request, errorResponse); 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 

      if (webViewPreviousState == PAGE_STARTED) { 

       if (null != loadingDialog) { 
        loadingDialog.dismiss(); 
        loadingDialog = null; 
       } 
      } 
     } 
    } 

isConnected方法用於檢查網絡連接

/** 
* Check if there is any connectivity 
* 
* @return is Device Connected 
*/ 
public boolean isConnected() { 

    ConnectivityManager cm = (ConnectivityManager) 
      this.getSystemService(Context.CONNECTIVITY_SERVICE); 

    if (null != cm) { 
     NetworkInfo info = cm.getActiveNetworkInfo(); 
     return (info != null && info.isConnected()); 
    } 

    return false; 

} 

正如你可以看到我已經添加了一個過濾器來檢查URL開始showalert:。如果我的Web客戶端類捕獲以showalert開頭的任何URL,則會觸發handleAlert方法。

結果

enter image description here

+0

感謝@HiteshSahu先生,在這裏我使用'SharedPreferences設置= getSharedPreferences(PREFS_NAME,0);',而不是'PreferenceManager'能否請您與我共享更新你的答案prefs的名字,因爲我已經以一種不同的方式將這些給予了一些活動,實際上它需要將文件保存在saredprefs的'MyPrefsFile1.xml'中,但現在保存爲my.example.webv_prefernce.xml,請幫助我解決這個問題...你的答案是工作,但我需要分配相同的價值差異選項..所以我已經添加,如果它的假打開活動與警報別打開活動ity ..但需要共享的首選項文件 –

+0

更新了答案。 –