Android WebView load pages project

1. AndroidManifest.xml file

Add Network Access

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

Set WebView full screen, hiding the top navigation bar.

In the present embodiment MainActivity inherited AppCompatActivity, the following settings can be hidden.

android:theme="@style/Theme.AppCompat.Light.NoActionBar"

Note: The inheritance Activity, FragmentActivity pages inside onCreate method added: requestWindowFeature (Window.FEATURE_NO_TITLE);

Settings page in the inside AndroidManifest theme: android: theme = "@ android: style / Theme.NoTitleBar" 

 

AndroidManifest.xml complete code. 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gsww.qyp.ms.mobser">
    <uses-permission android:name="android.permission.INTERNET"/>
    <!-- 打电话权限 -->
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <!--android:theme="@style/AppTheme"-->
    <application
        android:theme="@style/Theme.AppCompat.Light.NoActionBar"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        >
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

2. activity_main.xml file layout file

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <WebView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
</android.support.constraint.ConstraintLayout>

3. ManActivity.java file

In my page, there is a function call

<a href='tel://15300001111'>拨打电话</a>

Require special handling in MainActivity in setWebViewClient section.

package com.gsww.qyp.ms.mobser;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
    WebView myWebView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.loadUrl("http://192.168.32.120:8081/login");

        myWebView.getSettings().setJavaScriptEnabled(true);


        //加上下面这段代码可以使网页中的链接不以浏览器的方式打开
        myWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //判断用户单击的是那个超链接
                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("tel:"+mobile);
                    mIntent.setData(data);
                    //Android6.0以后的动态获取打电话权限
                    if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
                        startActivity(mIntent);
                        //这个超连接,java已经处理了,webview不要处理
                        return true;
                    } else {
                        //申请权限
                        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);
                        return true;
                    }
                } else { //点击其他链接
                    myWebView.loadUrl(url);
                }
                return true;
            }
        });
    }
    public boolean onKeyDown(int keyCode,KeyEvent event){
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (myWebView.canGoBack()) {
                myWebView.goBack();//返回上一界面
                return true;
            }else{
                System.exit(0);//退出程序
            }
        }
        return super.onKeyDown(keyCode,event);
    }
}

 

Published 95 original articles · won praise 216 · views 270 000 +

Guess you like

Origin blog.csdn.net/xukongjing1/article/details/82817425