Sometimes it is necessary to show some Web pages in the application, but it is clearly stated in the requirements that it is not allowed to open the system browser, obviously it is impossible to write a browser, then we need to use the WebView control, with which we can embed a browser in our own application. This makes it very easy to display a wide variety of web pages.
Because the program uses network functions, and access to the network is required to declare permissions, so first you have to modify the Androidmanifest.xml file, and add the claim permissions:
<android:name= "Android.permission.INTERNET"/>
Then modify the code in the Activity_main.xml as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" Match_parent " android:layout_height=" Match_parent " android:orientation= "vertical" > <ImageView android:id= "@+id/image_view" android:layout_width= "Match_parent" android:layout_height= "Match_parent"/> </LinearLayout>
Activity_main.xml
Finally, modify the code in Mainactivity as follows:
Public classMainactivityextendsappcompatactivity {PrivateWebView WebView; PrivateImageView ImageView; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.web_image); Setcontentview (R.layout.activity_main); WebView=(WebView) Findviewbyid (R.id.web_view); //Call the Setjavascriptenabled () method to let WebView support JS scriptWebview.getsettings (). setjavascriptenabled (true); //The jump page still appears in the current WebViewWebview.setwebviewclient (Newwebviewclient ()); Webview.loadurl ("Https://www.baidu.com"); }}
Call WebView's Loadurl () method, and the URL passed in, you can display the corresponding page content, here let us take a look at the homepage of Baidu's loading situation.
---------------------------- Load Network pictures dynamically -------------------------------
The display network picture is also very simple, probably the idea is obtains the picture address first, then accesses the picture through the network, then transfers the picture file to the bitmap object, finally displays it.
However, it is important to note that the process of acquiring a network picture needs to be run in a sub-thread, otherwise it is very easy to see the program is unresponsive (application not responding).
First, create a new Loadimagestask class, inherited from Asynctask, with the following code:
ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.os.AsyncTask;ImportAndroid.widget.ImageView;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;Importjava.net.MalformedURLException;ImportJava.net.URL; Public classLoadimagestaskextendsAsynctask<string, Void, bitmap> { PrivateImageView ImageView; PublicLoadimagestask (ImageView ImageView) { This. ImageView =ImageView; } @OverrideprotectedBitmap doinbackground (String ... params) {URL ImageUrl=NULL; Bitmap Bitmap=NULL; InputStream InputStream=NULL; Try{IMAGEURL=NewURL (params[0]); HttpURLConnection Conn=(HttpURLConnection) imageurl.openconnection (); Conn.setdoinput (true); Conn.connect (); InputStream=Conn.getinputstream (); Bitmap=Bitmapfactory.decodestream (InputStream); Inputstream.close (); } Catch(malformedurlexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } returnbitmap; } @Overrideprotected voidOnPostExecute (Bitmap Bitmap) {imageview.setimagebitmap (BITMAP); }}Loadimagestask.java
The layout code contains only one ImageView, and the code is a little bit.
Finally, the test is performed in mainactivity:
Public classMainactivityextendsappcompatactivity {PrivateImageView ImageView; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.web_image); String http= "Https://www.baidu.com/img/bd_logo1.png"; ImageView ImageView=(ImageView) Findviewbyid (R.id.image_view); //Start Asynchronous processing NewLoadimagestask (ImageView). Execute (HTTP); }}
Getting a picture of a network is not difficult, but asynchronous processing is difficult to understand, if you do not understand it can be consulted: Android asynchronous message processing mechanism analysis
Now let's look at a piece of loading, as follows:
Android local load page and display network picture