Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách lắng nghe sự kiện WebView tải xong URL trong Android

Ví dụ này minh họa cách theo dõi tiến trình tải và phát hiện khi WebView hoàn tất tải trang web bằng WebViewClient và WebChromeClient.

Bước 1: Tạo dự án mới

Tạo dự án mới trong Android Studio.

Bước 2: Cấu hình layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="#000000"
        android:padding="8dp"
        android:text="Nhấn nút để tải trang" />
    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/textView"
        android:layout_above="@id/btnLoad" />
    <Button
        android:id="@+id/btnLoad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Tải URL" />
</RelativeLayout>

Bước 3: Code MainActivity (src/MainActivity.java)

package com.app.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    WebView webView;
    TextView textView;
    Button btnLoad;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        textView = findViewById(R.id.textView);
        btnLoad = findViewById(R.id.btnLoad);
        webView = findViewById(R.id.webView);
        
        // Cho phép JavaScript
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        
        btnLoad.setOnClickListener(v -> {
            // WebViewClient: bắt sự kiện tải xong trang
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    Toast.makeText(MainActivity.this, "Đã tải xong: " + url, Toast.LENGTH_LONG).show();
                }
            });
            
            // WebChromeClient: theo dõi tiến trình tải (%)
            webView.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    textView.setText("Đang tải: " + newProgress + "%");
                    if (newProgress == 100) {
                        textView.setText("Đã tải xong trang web");
                    }
                }
            });
            
            webView.loadUrl("https://www.tutorialspoint.com");
        });
    }
}

Bước 4: Cấu hình Manifest (thêm quyền INTERNET)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">
    <uses-permission android:name="android.permission.INTERNET"/>
    <application ...>
        <activity android:name=".MainActivity">...</activity>
    </application>
</manifest>

Chạy ứng dụng

Nhấn nút "Tải URL", TextView sẽ hiển thị tiến trình % và Toast báo khi tải xong.

Cách lắng nghe sự kiện WebView tải xong URL trong Android Cách lắng nghe sự kiện WebView tải xong URL trong Android