facebook twitter hatena line email

Android/webview/useragent

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

サンプル

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();
}