how to get the default HTTP USER AGENT from the android device?
Asked Answered
V

4

17

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

thanks
Nohsib

Valverde answered 26/7, 2011 at 2:32 Comment(0)
G
10

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

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

Note that this User Agent will only apply for the embedded WebKit browser that's used by default in Android. Unfortunately, you'll need to create a new WebView object to get the user agent. Fortunately, the user agent doesn't change often, so you should only need to run this code once in your application lifetime (unless don't care about performance). Just do:

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

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

Governor answered 26/7, 2011 at 2:38 Comment(3)
Thankyou Oleg, if you could kindly share some code snippet on the same, would help.Valverde
Alright, I edited my response. (And next time, don't forget to do a search first.)Governor
why is it better?Bubo
B
37

as Varundroid mentioned in his answer,

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

is better way to do it for Android 2.1 and above.

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

From android source code.

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();
}   
Bedcover answered 28/1, 2013 at 23:46 Comment(2)
This is good, but if the scheme changes in future devices then it may not be accurate.Governor
The scheme is a spec, so you can add any values to it as long as it conforms the spec.Bedcover
G
10

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

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

Note that this User Agent will only apply for the embedded WebKit browser that's used by default in Android. Unfortunately, you'll need to create a new WebView object to get the user agent. Fortunately, the user agent doesn't change often, so you should only need to run this code once in your application lifetime (unless don't care about performance). Just do:

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

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

Governor answered 26/7, 2011 at 2:38 Comment(3)
Thankyou Oleg, if you could kindly share some code snippet on the same, would help.Valverde
Alright, I edited my response. (And next time, don't forget to do a search first.)Governor
why is it better?Bubo
D
3

When you use web view to access the user-agent, make sure you run the

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

on the UI thread.

If you want access the user agent on background thread. use

System.getProperty("http.agent")

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

Dishrag answered 30/5, 2018 at 17:10 Comment(0)
A
3

Android get User Agent

An alternative

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
    String userAgent = WebSettings.getDefaultUserAgent(context);
}
Asthmatic answered 8/10, 2018 at 10:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.