facebook twitter hatena line email

「Android/webview/useragent」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「==サンプル== WebView webView = new WebView(getApplicationContext()); final WebSettings settings = webView.getSettings(); Log.i("tag", settings.getUserAgentString()...」)
 
(サンプル)
 
行4: 行4:
 
  Log.i("tag", settings.getUserAgentString()); // 読み込み
 
  Log.i("tag", settings.getUserAgentString()); // 読み込み
 
  settings.setUserAgentString("Mozilla/5.0 (Linux; Android 8.0; Nexus 6P Build/OPP3.170518.006) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.121 Mobile Safari/537.36"); // 書き込み
 
  settings.setUserAgentString("Mozilla/5.0 (Linux; Android 8.0; Nexus 6P Build/OPP3.170518.006) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.121 Mobile Safari/537.36"); // 書き込み
 +
 +
==ua参考コード==
 +
参考:https://github.com/mopub/mopub-android-sdk/blob/master/mopub-sdk/mopub-sdk-base/src/main/java/com/mopub/network/Networking.java
 +
 +
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
 +
    userAgent = WebSettings.getDefaultUserAgent(context);
 +
} else if (Looper.myLooper() == Looper.getMainLooper()) {
 +
    userAgent = new WebView(context).getSettings().getUserAgentString();
 +
}

2018年12月14日 (金) 12:38時点における最新版

サンプル

WebView webView = new WebView(getApplicationContext());
final WebSettings settings = webView.getSettings();
Log.i("tag", settings.getUserAgentString()); // 読み込み
settings.setUserAgentString("Mozilla/5.0 (Linux; Android 8.0; Nexus 6P Build/OPP3.170518.006) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.121 Mobile Safari/537.36"); // 書き込み

ua参考コード

参考:https://github.com/mopub/mopub-android-sdk/blob/master/mopub-sdk/mopub-sdk-base/src/main/java/com/mopub/network/Networking.java

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    userAgent = WebSettings.getDefaultUserAgent(context);
} else if (Looper.myLooper() == Looper.getMainLooper()) {
    userAgent = new WebView(context).getSettings().getUserAgentString();
}