Thiết lập WebView

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

Hướng dẫn này cho bạn biết 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 API WebSettings để định cấu hình WebView cho:

  • JavaScript
  • Quyền truy cập vào bộ nhớ trên thiết 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 khung hiển thị web

Cookie và URL trang đóng vai trò quan trọng trong việc kiếm tiền từ lượt xem trên 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á hiệu suất của WebView, hãy tải nội dung web trực tiếp từ các URL dựa trên mạng. Tránh sử dụng WebViewAssetLoader, tải tài sản từ thiết bị hoặc tạo nội dung 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://webview-api-for-ads-test.glitch.me");
  }
}

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://webview-api-for-ads-test.glitch.me")
  }
}

Kiểm thử thành phần hiển thị trên web

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

https://webview-api-for-ads-test.glitch.me#webview-settings-tests

để xác minh rằng các chế độ cài đặt này có tác động như mong muốn đối với quảng cáo. URL thử nghiệm đáp ứng các tiêu chí thành công để tích hợp hoàn chỉnh nếu đáp ứng các điều kiện sau:

Chế độ cài đặt chế độ xem trên 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 cùng dòng và không mở trong trình phát tích hợp sẵn ở chế độ 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
  • Người dùng có thể phát lại quảng cáo dạng video

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