WebView获取视频地址链接

*****************************************************************************************
package com.example.webview;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    Button bt;
    EditText input;
    EditText jieguo;
    OwnWebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bt=(Button)findViewById(R.id.bt);
        input=(EditText)findViewById(R.id.input);
        jieguo=(EditText)findViewById(R.id.et);
        webView=(OwnWebView)findViewById(R.id.webView);


        //初始化WebView
        WebViewUtils.initWebSettings(webView, webViewClient);


        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(TextUtils.isEmpty(input.getText().toString().trim())){
                    webView.loadUrl(input.getText().toString());
                }else{
                    webView.loadUrl("https://www.baidu.com");
                }
            }
        });


    }


    private String finalUrl;
    private boolean t=true;

    WebViewClient webViewClient = new WebViewClient() {

        @Nullable
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
            if (url.contains(".mp4") || url.contains(".m3u8?") || url.contains(".avi") ||
                    url.contains(".mov") || url.contains(".mkv") || url.contains(".flv") ||
                    url.contains(".f4v") || url.contains(".rmvb")) {

                Log.e("urliiiiii", url);
               if(t){
                   if (!TextUtils.equals(finalUrl, url)) {
                       finalUrl = url;
                       Message msg = Message.obtain();
                       msg.what = 2;
                       msg.obj = finalUrl;
                       handler.sendMessage(msg);
                   }
               }
            }

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



    int is=0;

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            ///Log.w(TAG, "getHtmlcaonima: ****************");
            Log.e("urlllllllllllllllllllll", is+"  ***  "+message.obj);

            is++;
            jieguo.setText(is+"\n"+jieguo.getText()+"\n"+message.obj);
            webView.loadUrl(message.obj.toString());
            //t=false;
            return false;
        }
    });


}

*****************************************************************************************





*****************************************************************************************
package com.example.webview;

import android.annotation.SuppressLint;
import android.os.Build;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewUtils {

    /**
     * 初始化WebView
     */
    @SuppressLint("SetJavaScriptEnabled")
    public static void initWebSettings(WebView webView, WebViewClient client) {
        if (webView == null) {
            return;
        }
        //电脑UA,模拟谷歌浏览器
        String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36";
        WebSettings webSettings = webView.getSettings();
        //设置userAgent为电脑端的ua
//        webSettings.setUserAgentString(userAgent);
        //设置默认为utf-8
        webSettings.setDefaultTextEncodingName("UTF-8");
        //设置WebView中加载页面字体变焦百分比,默认100
        webSettings.setTextZoom(100);
        //属性可以让webview只显示一列,也就是自适应页面大小,不能左右滑动
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
        } else {
            webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        }

        //设置此属性,可任意比例缩放
        webSettings.setUseWideViewPort(false);
        webSettings.setLoadWithOverviewMode(true);

        //页面支持缩放
        webSettings.setBuiltInZoomControls(false);
        webSettings.setSupportZoom(false);
        webSettings.setDisplayZoomControls(false);
        //设置支持js
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSavePassword(false);
        //设置 缓存模式
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        // 开启 DOM storage API 功能
        webSettings.setDomStorageEnabled(true);
        //listView,webView中滚动拖动到顶部或者底部时的阴影
        webView.setOverScrollMode(View.OVER_SCROLL_NEVER);

        webView.setVerticalScrollBarEnabled(false);
        webView.setVerticalScrollbarOverlay(false);
        webView.setHorizontalScrollBarEnabled(false);
        webView.setHorizontalScrollbarOverlay(false);

        webView.setWebViewClient(client);



    }

}

*****************************************************************************************










*****************************************************************************************


package com.example.webview;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;



public class OwnWebView extends WebView {

    private Activity activity;

    public OwnWebView(Context context) {
        super(context);
        activity = (Activity) context;
    }

    public OwnWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        activity = (Activity) context;
    }

    public OwnWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        activity = (Activity) context;
    }

    @Override
    public void setOverScrollMode(int mode) {
        super.setOverScrollMode(mode);
        if (activity != null) {
            //WebView适配    
            
            //AutoSize.autoConvertDensityOfGlobal(activity);
        }
    }

}
*****************************************************************************************


在Module下的build.gradle 配置一下内容  AutoSize.autoConvertDensityOfGlobal(activity) 才可以用
*****************************************************************************************
dependencies {
//适配
            implementation 'me.jessyan:autosize:1.1.2'
}
*****************************************************************************************






AndroidManifest.xml

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


*****************************************************************************************


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.example.webview.OwnWebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="181dp" />
        <EditText
            android:id="@+id/input"
            android:layout_width="match_parent"
            android:layout_height="63dp"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="请输入链接" />

        <Button
            android:id="@+id/bt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="解析" />

        <EditText
            android:id="@+id/et"
            android:layout_width="match_parent"
            android:layout_height="439dp"
            android:ems="10"
            android:inputType="textMultiLine"
            android:text="解析出的链接" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

*****************************************************************************************

源码下载

https://download.csdn.net/download/huahuaxingjing/34216470 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值