Skip to content

WebView嗅探

1.1 参考文档

webView相关基础知识,查看博客:

https://www.jianshu.com/p/3c94ae673e2a

腾讯X5文档,查看文档:

https://x5.tencent.com/docs/index.html

使用案例

2.1 使用步骤

1.使用Android Studio创建项目,选择空页面,选择java语言,groovy。创建项目

2.创建成功后,修改配置文件

AndroidManifest.xml

xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:usesCleartextTraffic="true"
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.WebViewDemo"
        tools:targetApi="31">
        <activity
            android:name=".WebViewActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.WebViewDemo">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

在原始页面的基础上,添加网络权限

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

指定app应用入口为WebViewActivity

xml
<activity
            android:name=".WebViewActivity"
          ></activity>

添加WebViewActivity.java文件

java
package com.example.webviewdemo;

import android.os.Bundle;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class WebViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 从Intent中提取参数
        // String playUrl = getIntent().getStringExtra("PARAMETER_KEY");
        setContentView(R.layout.activity_web_view);
        //获得控件
        WebView webView = (WebView) findViewById(R.id.wv_webview);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        //访问网页
        webView.loadUrl("https://www.freeok.pro/xplay/66660-1-1.html");
        //系统默认会通过手机浏览器打开网页,为了能够直接通过WebView显示网页,则必须设置
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                String html = view.getOriginalUrl() != null ? view.getOriginalUrl() : view.getUrl();
                // 调用方法获取HTML
                Log.i("html", html);
            }
            @Override
            public void onLoadResource(WebView view, String url) {
                Log.i("soeasy", url);
                //设定加载资源的操作
                if (url.contains(".mp4") || url.contains(".m3u8")) {
                    Toast.makeText(WebViewActivity.this, url, Toast.LENGTH_LONG);
                    Log.i("so-easy", url);
                }
            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //使用WebView加载显示url
                view.loadUrl(url);
                //返回true
                return true;
            }
        });
    }
}