Retrieve User-Agent programmatically

Laimoncijus picture Laimoncijus · Sep 2, 2010 · Viewed 37.8k times · Source

Is there a way to retrieve Browser's user-agent without having a WebView in activity?

I know it is possible to get it via WebView:

WebView view = (WebView) findViewById(R.id.someview);
String ua = view.getSettings().getUserAgentString() ;

But in my case I don't have/need a webview object and I don't want to create it just for retrieving user-agent string.

Answer

Craig Russell picture Craig Russell · Sep 7, 2012

There is a much simpler way if you are on Android 2.1 or above. Granted this isn't the exact same User Agent string that a webview would return, but might serve you well enough for your purposes.

As an additional advantage to pulling from web view, you can use this from any thread (not just the UI thread).

There is a system property called http.agent, which can be used to retrieve the User-Agent string.

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

See Programmatically get User-Agent String for more details.