Thiết lập chế độ xem web

Chọn nền tảng: Android (beta) Mới Android iOS

Nếu ứng dụng của bạn sử dụng WebView để hiển thị nội dung web, thì bạn nên định cấu hình để có thể kiếm tiền tối ưu hoá từ nội dung bằng quảng cáo.

Hướng dẫn này trình bày cách cung cấp thông tin về cách định cấu hình đối tượng WebView.

Bật cookie của bên thứ ba

Để cải thiện trải nghiệm quảng cáo của người dùng và để nhất quán với chính sách cookie của Chrome, hãy bật cookie của bên thứ ba trên thực thể WebView của bạn.

Java

CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);

Kotlin

CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)

Cài đặt web

Chế độ cài đặt WebView mặc định không được tối ưu hoá cho quảng cáo. Sử dụng WebSettings API để định cấu hình WebView cho:

  • JavaScript
  • Quyền truy cập vào bộ nhớ cục bộ
  • Tự động phát video

Java

import android.webkit.CookieManager;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {
  private WebView webView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = findViewById(R.id.webview);

    // Let the web view accept third-party cookies.
    CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
    // Let the web view use JavaScript.
    webView.getSettings().setJavaScriptEnabled(true);
    // Let the web view access local storage.
    webView.getSettings().setDomStorageEnabled(true);
    // Let HTML videos play automatically.
    webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
  }
}

Kotlin

import android.webkit.CookieManager
import android.webkit.WebView

class MainActivity : AppCompatActivity() {
  lateinit var webView: WebView

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    webView = findViewById(R.id.webview)

    // Let the web view accept third-party cookies.
    CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
    // Let the web view use JavaScript.
    webView.settings.javaScriptEnabled = true
    // Let the web view access local storage.
    webView.settings.domStorageEnabled = true
    // Let HTML videos play automatically.
    webView.settings.mediaPlaybackRequiresUserGesture = false
  }
}

Tải nội dung chế độ xem web

Cookie và URL trang là những yếu tố quan trọng để kiếm tiền từ chế độ xem web và chỉ hoạt động như mong đợi khi loadUrl() được sử dụng với URL dựa trên mạng. Để tối ưu hoá WebView hiệu suất, hãy tải nội dung trên web trực tiếp từ URL dựa trên mạng. Tránh sử dụng WebViewAssetLoader, tải thành phần từ thiết bị hoặc tạo nội dung trên web một cách linh động.

Java

import android.webkit.CookieManager;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {
  private WebView webView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = findViewById(R.id.webview);

    // Let the web view accept third-party cookies.
    CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
    // Let the web view use JavaScript.
    webView.getSettings().setJavaScriptEnabled(true);
    // Let the web view access local storage.
    webView.getSettings().setDomStorageEnabled(true);
    // Let HTML videos play automatically.
    webView.getSettings().setMediaPlaybackRequiresUserGesture(false);

    // Load the URL for optimized web view performance.
    webView.loadUrl("https://google.github.io/webview-ads/test/");
  }
}

Kotlin

import android.webkit.CookieManager
import android.webkit.WebView

class MainActivity : AppCompatActivity() {
  lateinit var webView: WebView

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    webView = findViewById(R.id.webview)

    // Let the web view accept third-party cookies.
    CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
    // Let the web view use JavaScript.
    webView.settings.javaScriptEnabled = true
    // Let the web view access local storage.
    webView.settings.domStorageEnabled = true
    // Let HTML videos play automatically.
    webView.settings.mediaPlaybackRequiresUserGesture = false

    // Load the URL for optimized web view performance.
    webView.loadUrl("https://google.github.io/webview-ads/test/")
  }
}

Kiểm thử chế độ xem web

Trong quá trình phát triển ứng dụng, bạn nên tải URL kiểm thử này:

https://google.github.io/webview-ads/test/

để xác minh rằng các chế độ cài đặt này có tác động như dự kiến đối với quảng cáo. URL kiểm thử có tiêu chí thành công để tích hợp hoàn chỉnh nếu bạn quan sát thấy những điều sau:

Cài đặt chế độ xem web

  • Cookie của bên thứ ba hoạt động
  • Cookie của bên thứ nhất hoạt động
  • Đã bật JavaScript
  • Đã bật bộ nhớ DOM

Quảng cáo dạng video

  • Quảng cáo dạng video phát nội tuyến và không mở trong trình phát tích hợp toàn màn hình
  • Quảng cáo dạng video tự động phát mà không cần nhấp vào nút phát
  • Quảng cáo dạng video có thể phát lại

Sau khi kiểm thử xong, hãy thay thế URL kiểm thử bằng URL mà chế độ xem web dự định tải.