Andrews phone simulator

[Ten percent Emblem: XSX1346 continually updated] [] [] [wide variety of randomly selected] [quality assurance]

So before you open the application needs to detect what network, if it is web-side, then it can not detect a network, so consider the application of H5 using Android webview package it.

1, configure the network connection rights

 

In AndroidManifest.xml file, add the following configuration information

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

Note: Starting with Android 9.0 (API level 28), is disabled by default in plain text support. Thus the url http are not loaded in the webview, so only the configuration information above may result in net :: ERR_CLEARTEXT_NOT_PERMITTED error, also you need to add the following configuration in the application configuration file. Referring to article: net :: ERR_CLEARTEXT_NOT_PERMITTED

 

android:usesCleartextTraffic="true"

webview abnormal network configuration

net::ERR_CLEARTEXT_NOT_PERMITTED 配置

2, create a layout file

Use idea, then automatically creates MainActivity and corresponding layout file, edit it directly on the basis of the file, use Webview control, if you need to use the progress bar, then, ProgressBar configuration can open it.

 

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout

        xmlns:android="http://schemas.android.com/apk/res/android"

        xmlns:tools="http://schemas.android.com/tools"

        xmlns:app="http://schemas.android.com/apk/res-auto"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        tools:context="com.ctjsoft.jxf.shop.MainActivity">

    <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent"/>

    <!--<ProgressBar android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/progressbar"

                 style="@android:style/Widget.ProgressBar.Horizontal" android:max="100" android:progress="0"

                 android:visibility="gone"/>-->

</androidx.constraintlayout.widget.ConstraintLayout>

3, modify the file MainActivity

Override the onCreate method:

 

protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate (savedInstanceState);

        setContentView(R.layout.activity_main);

        // progressBar = (ProgressBar) findViewById (R.id.progressbar); // progress bar

 

        webView = (WebView) findViewById(R.id.webview);

        webView.getSettings().setAllowUniversalAccessFromFileURLs(true);

        webView.getSettings().setAllowFileAccessFromFileURLs(true);

        // webview setup add the following code

        try {

            if (Build.VERSION.SDK_INT >= 16) {

                Class<?> clazz = webView.getSettings().getClass();

                Method method = clazz.getMethod("setAllowUniversalAccessFromFileURLs", boolean.class);

                if (method != null) {

                    method.invoke(webView.getSettings(), true);

                }

            }

        } catch (IllegalArgumentException e) {

            e.printStackTrace ();

        } catch (NoSuchMethodException e) {

            e.printStackTrace ();

        } catch (IllegalAccessException e) {

            e.printStackTrace ();

        } catch (InvocationTargetException e) {

            e.printStackTrace ();

        }

        //webView.loadUrl("http://172.17.1.176:8082/");// loading url

        webView.loadUrl(API);

 

        // display the html code using webview

// webView.loadDataWithBaseURL(null,"<html><head><title> 欢迎您 </title></head>" +

// "<body><h2>使用webview显示 html代码</h2></body></html>", "text/html" , "utf-8", null);

 

        webView.addJavascriptInterface (this, "android"); // add this html js listeners will be able to call the client

        webView.setWebChromeClient (webChromeClient);

        webView.setWebViewClient(webViewClient);

        WebSettings webSettings = webView.getSettings();

        /**

         * LOAD_CACHE_ONLY: do not use the network, only read the local cache data

         * LOAD_DEFAULT: (default) to decide whether to fetch data from the network in accordance with cache-control.

         * LOAD_NO_CACHE: do not use the cache, only to get data from the network.

         * LOAD_CACHE_ELSE_NETWORK, as long as there is local, whether expired or no-cache, use the data in the cache.

         */

        webSettings.setCacheMode (WebSettings.LOAD_DEFAULT); // do not use the cache, only the data acquired from the network.

        webView.getSettings().setTextZoom(100);

webView.getSettings () setJavaScriptCanOpenWindowsAutomatically (true);. // set js can open a window, such as the window.open (), default is false

        webView.getSettings () setJavaScriptEnabled (true);. // whether to allow execution js, default is false. When set true, it will remind cause XSS vulnerabilities

        webView.getSettings () setSupportZoom (true);. // Can zoom, default

        webView.getSettings () setBuiltInZoomControls (true);. // whether to display the zoom button, the default false

        webView.getSettings () setUseWideViewPort (true);. // set this property, it can be arbitrarily scaled. Large view mode

        webView.getSettings () setLoadWithOverviewMode (true);. // and setUseWideViewPort (true) to solve the problem with adaptive pages

        . WebView.getSettings () setAppCacheEnabled (true); // whether to use the cache

        webView.getSettings().setDomStorageEnabled(true);//DOM Storage

    }

  ```

Configuration WebviewClient

// WebViewClient major help WebView process notifications, request event

private WebViewClient webViewClient = new WebViewClient() {

    @Override

    public void onPageFinished (WebView view, String url) {// page loads

        //progressBar.setVisibility(View.GONE);

    }

 

    public void onPageStarted (WebView view, String url, Bitmap favicon) {// page starts loading

        //progressBar.setVisibility(View.VISIBLE);

    }

 

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)

    @Override

    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

        Log.i("ansen", "拦截url:" + request.getUrl());

        return super.shouldOverrideUrlLoading(view, request);

    }

 

};

 

// WebChromeClient major secondary processing Javascript WebView dialog box, the logo, website title, such as loading progress

private WebChromeClient webChromeClient = new WebChromeClient() {

    // do not support js the alert pop, so you need to listen and then through the dialog pop

    public boolean onJsAlert(WebView webView, String url, String message, JsResult result) {

        AlertDialog.Builder localBuilder = new AlertDialog.Builder(webView.getContext());

        localBuilder.setMessage(message).setPositiveButton("确定", null);

        localBuilder.setCancelable (false);

        localBuilder.create().show();

 

        //note:

        // This must be a code: result.confirm () said:

        // result of the processing to determine the state of threads simultaneously wake WebCore

        // or can not continue to click the button

        result.confirm();

        return true;

    }

 

    // Get the page title

    @Override

    public void onReceivedTitle(WebView view, String title) {

        super.onReceivedTitle(view, title);

        Log.i ( "ansen", "page headline:" + title);

    }

 

    // callback loading progress

    @Override

    public void onProgressChanged(WebView view, int newProgress) {

        // progressBar.setProgress(newProgress);

    }

};

 

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

    Log.i ( "ansen", "Is there a page:" + webView.canGoBack ());

    if (webView.canGoBack () && keyCode == KeyEvent.KEYCODE_BACK) {// click the Back button when there is no previous judgment

        webView.goBack (); // goBack () represents the return of the previous page webView

        return true;

    }

    return super.onKeyDown(keyCode, event);

}

 

/**

 * JS method call android

 *

 * @Param Cycle

 * @return

 */

@JavascriptInterface // still essential

public void getClient(String str) {

Guess you like

Origin www.cnblogs.com/jsbjsh/p/12615110.html