Pārlūkot izejas kodu

1.修改Android Apex CRM返回键拦截。

Pen Li 7 gadi atpakaļ
vecāks
revīzija
567e909469

+ 51 - 0
ApexDrivers/apexcrm/src/main/java/com/usai/apex/apexcrm/MainActivity.java

@@ -18,6 +18,7 @@ import android.support.v7.app.AlertDialog;
 import android.support.v7.app.AppCompatActivity;
 import android.text.TextUtils;
 import android.util.Log;
+import android.view.KeyEvent;
 import android.view.MenuItem;
 import android.view.View;
 import android.webkit.SslErrorHandler;
@@ -171,6 +172,56 @@ public class MainActivity extends AppCompatActivity implements RAWebView.WebView
         }
     }
 
+    @Override
+    public boolean onKeyDown(int keyCode, KeyEvent event) {
+        if (keyCode == KeyEvent.KEYCODE_BACK) {
+
+            executeGoBackJS();
+            return true;
+        }
+        return super.onKeyDown(keyCode, event);
+    }
+
+    private void executeGoBackJS() {
+
+        String js = "goBack()";
+        mWebView.evaluateJavascript(js, new ValueCallback<String>() {
+            @Override
+            public void onReceiveValue(String value) {
+
+                Log.d(TAG, "GoBack ReceiveValue: " + value);
+
+                if (value.equals("false")) {
+
+                    runOnUiThread(new Runnable() {
+                        @Override
+                        public void run() {
+                            new AlertDialog.Builder(mCtx)
+                                    .setTitle("Warning")
+                                    .setMessage("are you sure to exit?")
+                                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
+                                        @Override
+                                        public void onClick(DialogInterface dialog, int which) {
+                                            finish();
+                                        }
+                                    })
+                                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
+                                        @Override
+                                        public void onClick(DialogInterface dialog, int which) {
+
+                                        }
+                                    })
+                                    .show();
+                        }
+                    });
+
+                } else if (value.equals("true")) {
+
+                }
+            }
+        });
+    }
+
     private void clickReload() {
         mEmptyContainer.setVisibility(View.GONE);
         mWebView.loadUrl(mURL);

+ 13 - 0
ApexDrivers/apexcrm/src/main/java/com/usai/apex/apexcrm/RAWebView.java

@@ -113,6 +113,19 @@ public class RAWebView extends RelativeLayout {
         mCtx = context;
     }
 
+    public boolean canGoBack() {
+        if (mWebView != null) {
+            return mWebView.canGoBack();
+        }
+        return false;
+    }
+
+    public void goBack() {
+        if (mWebView != null) {
+            mWebView.goBack();
+        }
+    }
+
     private void initWebSetting() {
         if (mWebView != null) {
             //声明WebSettings子类