Android Webview登录状态检查

时间:2020-07-02 02:30:34

标签: android android-studio android-fragments webview android-webview

我正在尝试开发一个Android应用程序以使用webView加载我们的Web应用程序。该屏幕包含常规身份验证。登录成功后,我需要加载另一个URL地址,而不是该Web应用程序的实际主页。我在onPageFinished内部完成了此操作。但是仅在显示主屏幕后才加载第二个URL。请提出解决方案,以避免这种情况。 请找到以下代码示例:

public class LoginPage extends AppCompatActivity {
    /********** For Camera  ************/
    /*-- you can customize these options for your convenience --*/
    private static String webview_url = "initial url";
    private static String initialURL = "Main url";
    private static String file_type = "image/*";    // file types to be allowed for upload
    private boolean multiple_files = true;         // allowing multiple file upload
    private static final String TAG = LoginPage.class.getSimpleName();
    /*-- MAIN VARIABLES --*/
    private String cam_file_data = null;        // for storing camera file information
    private ValueCallback<Uri> file_data;       // data/header received after file selection
    private ValueCallback<Uri[]> file_path;     // received file(s) temp. location
    private final static int file_req_code = 1;
    /********** For Camera ************/

    LinearLayout linearLayout;
    Dialog dialog;
    Button contnue;
    AlertDialog alertDialog;
    WebView mWebview;

    private HomeViewModel homeViewModel;

    private PermissionRequest mPermissionRequest;
    private ProgressBar spinner;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_page);
        mWebview = findViewById(R.id.mWebview);
        spinner = (ProgressBar) findViewById(R.id.progressBar);
        spinner.setVisibility(View.VISIBLE);
        ///
        Log.d("Login  ", "Login  ");
        _setAndLoadWebView();
    }//on create

    private void _setAndLoadWebView(){
        assert mWebview != null;
        mWebview.setVisibility(View.VISIBLE);
        WebSettings webSettings = mWebview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true);
        if (Build.VERSION.SDK_INT >= 21) {
            webSettings.setMixedContentMode(0);
            mWebview.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        } else {
            mWebview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }

        mWebview.getSettings().setDomStorageEnabled(true);
        // Other webview options
        mWebview.getSettings().setLoadWithOverviewMode(true);

        //mWebview.getSettings().setUseWideViewPort(true);
        //webView.getSettings().setUseWideViewPort(true);

        //Other webview settings
        mWebview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        mWebview.setScrollbarFadingEnabled(false);
        mWebview.getSettings().setBuiltInZoomControls(true);
        mWebview.getSettings().setPluginState(WebSettings.PluginState.ON);
        mWebview.getSettings().setAllowFileAccess(true);
        mWebview.getSettings().setSupportZoom(true);

        mWebview.setWebViewClient(new _webViewClientCallback());
        //mWebview.loadUrl(webview_url);
        mWebview.setWebChromeClient(new _WebChromeClient());

        mWebview.loadUrl(initialURL);

    }//_setAndLoadWebView

    /*-- callback reporting if error occurs --*/
    public class _webViewClientCallback extends WebViewClient {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(LoginPage.this, "Failed loading app!", Toast.LENGTH_SHORT).show();
        }

        @TargetApi(android.os.Build.VERSION_CODES.M)
        @Override
        public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
            // Redirect to deprecated method, so you can use it in all SDK versions
            onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
        }

        //If you will not use this method url links are open in new brower not in webview
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // Stay within this webview and load url
            view.loadUrl(url);
            return true;
        }//shouldOverrideUrlLoading

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed(); // When an error occurs, ignore and go on
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (url.startsWith(initialURL)) {
                spinner.setVisibility(View.GONE);
                mWebview.loadUrl(webview_url);
            }
            spinner.setVisibility(View.GONE);
        }
    }//Callback

    /*-- Chrome client class --*/
    public class _WebChromeClient extends WebChromeClient {
        /*-- handling input[type="file"] requests for android API 21+ --*/
        public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {

            if (file_permission() && Build.VERSION.SDK_INT >= 21) {
                file_path = filePathCallback;
                Intent takePictureIntent = null;
                boolean includePhoto = true;

                if (includePhoto) {
                    takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                        File photoFile = null;
                        try {
                            photoFile = create_image();
                            takePictureIntent.putExtra("PhotoPath", cam_file_data);
                        } catch (IOException ex) {
                            Log.e(TAG, "Image file creation failed", ex);
                        }
                        if (photoFile != null) {
                            cam_file_data = "file:" + photoFile.getAbsolutePath();
                            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                        } else {
                            cam_file_data = null;
                            takePictureIntent = null;
                        }
                    }
                }

                Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
                contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
                contentSelectionIntent.setType(file_type);
                if (multiple_files) {
                    contentSelectionIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                }

                Intent[] intentArray;
                if (takePictureIntent != null) {
                    intentArray = new Intent[]{takePictureIntent};
                } else {
                    intentArray = new Intent[0];
                }

                Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
                chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
                chooserIntent.putExtra(Intent.EXTRA_TITLE, "File chooser");
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
                startActivityForResult(chooserIntent, file_req_code);
                return true;
            } else {
                return false;
            }
        }

    }

请帮助我找到更好的解决方案。

0 个答案:

没有答案
相关问题