Android Studio Lesson 11 Network Programming 3


πŸ“‹ About the author: Hello friends, I am the Chaos Cape Hammer, one of the sophomore students
πŸ’– Author homepage: The personal blog page of the Chaos Cape Hammer. [Jump to my profile]
πŸ”₯ I like to edit like my friends and I type code every day 🀭, addicted to study, losing weight day by day. I am honored to share what I have learned with you and make progress with you. If the article is wrong, welcome to correct it in the comment area. So start learning
today!
πŸŽ‰ Support me: LikeπŸ‘+ Favorite ⭐️+ Comment πŸ“

web page embedded in Android Studio

1. Preparations, enable network [access]

AndroidManifest.xml added

Second, build [gradle] guide package

add in build gradle

viewBinding{
        enabled=true
    }

3. Add html code

As shown in the figure, first create a folder, and then add the html file into it

Fourth, the core code

The incoming method can be in two ways: relative path and http web page

setContentView(R.layout.activity_main);
        activityMainBinding.textView.setText( "Hunan University of Traditional Chinese Medicine" );
        // activityMainBinding.webView.loadUrl("http://121.4.44.56/test.html"); 
        activityMainBinding.webView.loadUrl( "file:///android_asset /test.html\n" );
    activityMainBinding.webView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return super.shouldOverrideUrlLoading(view, url);
           // return  true;
        }
    });
    activityMainBinding.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            activityMainBinding.webView.goBack();
        }
    });

        WebSettings webSettings = activityMainBinding.webView.getSettings();
//If you want to interact with Javascript in the visited page, the webview must be set to support Javascript 
        webSettings.setJavaScriptEnabled( true );

//Set the adaptive screen, use both 
        webSettings.setUseWideViewPort( true ); //Adjust the picture to the size of the webview 
        webSettings.setLoadWithOverviewMode( true ); //Zoom to the size of the 
screen//Adaptive screen
        activityMainBinding.webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setLoadWithOverviewMode( true ); // zoom to the size of the screen

//Zoom operation 
        webSettings.setSupportZoom( true ); //Support zoom, the default is true. It is the premise of the following. 
        webSettings.setBuiltInZoomControls( true ); //Set the built-in zoom controls. If false, the WebView cannot be zoomed 
        webSettings.setDisplayZoomControls( false ); //Hide the native zoom controls

//Other detailed operations 
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //Close the cache in the webview 
        webSettings.setAllowFileAccess( true ); //Set the access file 
        webSettings.setJavaScriptCanOpenWindowsAutomatically( true ); //Support to open new windows through JS 
        webSettings. setLoadsImagesAutomatically( true ); //Support automatic loading of images 
        webSettings.setDefaultTextEncodingName( "utf-8" ); //Set the encoding format

//Use the cache first
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
//The cache mode is as follows: 
//LOAD_CACHE_ONLY: Do not use the network, only read the local cache data 
//LOAD_DEFAULT: (default) Determine whether to fetch data from the network according to cache-control. 
//LOAD_NO_CACHE: Do not use cache, only get data from the network. 
//LOAD_CACHE_ELSE_NETWORK, as long as it is available locally, whether it is expired or no-cache, use the data in the cache 
//Do not use the cache 
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE) ;

Summarize

Alright, that’s it for today’s study. Friends who think it is good can give a follow, like or favorite! Thank you friends for your support. The following code hopes that you guys can check it yourself, after all, the hands-on operation makes the memory more profound.

Leave a Comment

Your email address will not be published. Required fields are marked *