如果您的應用程式 Android 使用
WebView
來顯示網路內容
考慮最佳化點擊行為的原因如下:
WebView
不支援自訂網址配置 如果點擊到達網頁前往另一個應用程式,廣告可能會傳回 。 舉例來說,Google Play 到達網址可能會使用market://
。
- Google 登入
和 Facebook 登入
不
支援
WebView
。
本指南提供建議的步驟,協助您改善行動裝置上的點擊行為 網頁檢視,同時保留網頁檢視內容。
必要條件
導入作業
請按照下列步驟,將網站上的點擊行為最佳化
WebView
執行個體:
覆寫
shouldOverrideUrlLoading()
WebViewClient
。 當網址即將在目前的中載入時,系統就會呼叫此方法WebView
。決定是否要覆寫點擊網址的行為。
下列程式碼片段檢查目前的網域是否與 目標網域。這只是一種方法,因為您使用的條件各有不同。
決定是否要在外部瀏覽器開啟網址 (Android 自訂) 分頁或 現有的網頁檢視畫面本指南說明如何開啟「離開」頁面的網址 即可在網站上安裝 Android 自訂分頁
程式碼範例
首先,將 androidx.browser
依附元件新增至模組層級 build.gradle
檔案,通常為 app/build.gradle
。自訂分頁的必要步驟:
dependencies {
implementation 'androidx.browser:browser:1.5.0'
}
下列程式碼片段說明如何實作 shouldOverrideUrlLoading()
:
Java
public class MainActivity extends AppCompatActivity {
private WebView webView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ... Register the WebView.
webView = new WebView(this);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(
new WebViewClient() {
// 1. Implement the web view click handler.
@Override
public boolean shouldOverrideUrlLoading(
WebView view,
WebResourceRequest request) {
// 2. Determine whether to override the behavior of the URL.
// If the target URL has no host, return early.
if (request.getUrl().getHost() == null) {
return false;
}
// Handle custom URL schemes such as market:// by attempting to
// launch the corresponding application in a new intent.
if (!request.getUrl().getScheme().equals("http")
&& !request.getUrl().getScheme().equals("https")) {
Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
// If the URL cannot be opened, return early.
try {
MainActivity.this.startActivity(intent);
} catch (ActivityNotFoundException exception) {
Log.d("TAG", "Failed to load URL with scheme:" + request.getUrl().getScheme());
}
return true;
}
String currentDomain;
// If the current URL's host cannot be found, return early.
try {
currentDomain = new URI(view.getUrl()).toURL().getHost();
} catch (URISyntaxException | MalformedURLException exception) {
// Malformed URL.
return false;
}
String targetDomain = request.getUrl().getHost();
// If the current domain equals the target domain, the
// assumption is the user is not navigating away from
// the site. Reload the URL within the existing web view.
if (currentDomain.equals(targetDomain)) {
return false;
}
// 3. User is navigating away from the site, open the URL in
// Custom Tabs to preserve the state of the web view.
CustomTabsIntent intent = new CustomTabsIntent.Builder().build();
intent.launchUrl(MainActivity.this, request.getUrl());
return true;
}
});
}
}
Kotlin
class MainActivity : AppCompatActivity() {
private lateinit var webView: WebView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// ... Register the WebView.
webView.webViewClient = object : WebViewClient() {
// 1. Implement the web view click handler.
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
// 2. Determine whether to override the behavior of the URL.
// If the target URL has no host, return early.
request?.url?.host?.let { targetDomain ->
val currentDomain = URI(view?.url).toURL().host
// Handle custom URL schemes such as market:// by attempting to
// launch the corresponding application in a new intent.
if (!request.url.scheme.equals("http") &&
!request.url.scheme.equals("https")) {
val intent = Intent(Intent.ACTION_VIEW, request.url)
// If the URL cannot be opened, return early.
try {
this@MainActivity.startActivity(intent)
} catch (exception: ActivityNotFoundException) {
Log.d("TAG", "Failed to load URL with scheme: ${request.url.scheme}")
}
return true
}
// If the current domain equals the target domain, the
// assumption is the user is not navigating away from
// the site. Reload the URL within the existing web view.
if (currentDomain.equals(targetDomain)) {
return false
}
// 3. User is navigating away from the site, open the URL in
// Custom Tabs to preserve the state of the web view.
val customTabsIntent = CustomTabsIntent.Builder().build()
customTabsIntent.launchUrl(this@MainActivity, request.url)
return true
}
return false
}
}
}
}
測試網頁瀏覽
如要測試網頁導覽變更,請載入
https://webview-api-for-ads-test.glitch.me#click-behavior-tests
顯示在網頁檢視畫面中按一下各種連結類型,即可瞭解連結方式 在應用程式中的行為。
建議您確認以下事項:
- 每個連結都會開啟目標網址。
- 返回應用程式時,測試頁面的計數器不會重設為零至 並確認網頁狀態是否已保留。