How to get the default HTTP USER AGENT from an Android device?

How to get the default HTTP USER AGENT and its default settings from an Android device?

thanks
Nohsib

+9
android
source share
3 answers

Edit: see Prakash's answer, which is better for 2.1+.

Try http://developer.android.com/reference/android/webkit/WebSettings.html#getUserAgentString

Please note that this User Agent will only be used for the built-in WebKit browser, which is used by default in Android. Unfortunately, you need to create a new WebView object to receive a user agent. Fortunately, the user agent does not change often, so you only need to run this code once in your application life (if you do not care about performance). Just do:

String userAgent = new WebView(this).getSettings().getUserAgentString(); 

Alternatively, you can use the JavaScript navigator.getUserAgent() method.

+6
source

as Varundrodge mentioned in his answer,

 String userAgent = System.getProperty("http.agent"); 

is the best way to do this for Android 2.1 and higher.

=====================

From the source code of Android.

 public static String getDefaultUserAgent() { StringBuilder result = new StringBuilder(64); result.append("Dalvik/"); result.append(System.getProperty("java.vm.version")); // such as 1.1.0 result.append(" (Linux; U; Android "); String version = Build.VERSION.RELEASE; // "1.0" or "3.4b5" result.append(version.length() > 0 ? version : "1.0"); // add the model for the release build if ("REL".equals(Build.VERSION.CODENAME)) { String model = Build.MODEL; if (model.length() > 0) { result.append("; "); result.append(model); } } String id = Build.ID; // "MASTER" or "M4-rc20" if (id.length() > 0) { result.append(" Build/"); result.append(id); } result.append(")"); return result.toString(); } 
+27
source

When you use web browsing to access a user agent, make sure that you run

new WebView(this).getSettings().getUserAgentString();

in the user interface thread.

If you want to access the user agent in the background thread. using

System.getProperty("http.agent")

To check if agent-agent is valid or not, use this https://deviceatlas.com/device-data/user-agent-tester

+1
source

All Articles