WebView is simple to use: there is a phone in the web page, click on the client to make a call

One: The layout file activity_main.xml in layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

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

</RelativeLayout>

 Two: the code in MainActivity

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MainActivity extends Activity {
//webView is equivalent to the browser declaring webView
    WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);
        //Initialize the webview control
        webView=(WebView) findViewById(R.id.webView);
        //The URL of the web page address that needs to be displayed on the mobile client
        webView.loadUrl("http://172.00.00.212:8080/manager/myhtml/mall.html");
        //No, click the hyperlink, start the browser of the system, and display the web page in our own APP after adding it.
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading
            (WebView view, String url) {
                Log.i("User clicks on hyperlink", url);
                //Determine which hyperlink the user clicked
               String tag = "tel:";
              if (url.contains(tag)) {
                   String mobile = url.substring(url.lastIndexOf("/") + 1);
                    Intent mIntent = new Intent(Intent.ACTION_CALL);
                    Uri data = Uri.parse (mobile);
                    mIntent.setData(data);
	    // Dynamically obtain call permission after Android 6.0
            if (ActivityCompat.checkSelfPermission(MainActivity.this,
                android.Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(mIntent);//This hyperlink has been processed by java, webview should not process it
                return true;
            }else{
		//request for access
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE},1);
                return true;
            }
        }
        return true;
    }

                return super.shouldOverrideUrlLoading(view, url);
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

 

 Three: Web pages to be displayed in advance

 

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>

    <body>
        <table bgcolor="greenyellow" border="0" width="100%" height="100">
            <tr>
                <td colspan="3" align="center">
                    <h1>商城</h1></td>
            </tr>
            <tr>
                <td>Home</td>
                <td>商品</td>
                <td><a href="index.html">订单</a> </td>
            </tr>
        </table>

        <table bgcolor="gray" width="100%">
            <tr>
                <td bgcolor="aqua" width="100">
                    <table>
                        <tr>
                            <td>Category 1</td>
                        </tr>
                        <tr>
                            <td>Category 2</td>
                        </tr>
                    </table>
                </td>
                <td>
                    <table>
                        <tr>
                            <td><img src="img/11.jpg" width="200"></td>
                        </tr>
                        <tr>
                            <td><img src="img/12.jpg" width="200"></td>
                        </tr>
                    </table>

                </td>
            </tr>
        </table>

        <table bgcolor="greenyellow" width="100%" height="60">
            <tr><a href="tada:tel/13698888">Contact number: one hour delivery</a>
            <a href="tarena:writedb/1#java&2#anaroid">保存</a>
            </tr>
        </table>
    </body>

</html>

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326095097&siteId=291194637
Recommended