Is there a way to obtain the default user agent string aside from WebView.getSettings().getUserAgentString()?
Solution 1
Very late answer, for others that may be looking for this.
I was looking for a way to obtain the user agent string used by HttpUrlConnection, to use it with HttpClient and amend it with my own version info. This way, my Android app provides some useful version info I can extract from the server's log files (Android Version, device name/type, and the version of my app).
For example, the user agent string for my phone when using HttpUrlConnection looks like this:
Dalvik/1.4.0 (Linux; U; Android 2.3.5; HTC Desire HD A9191 Build/GRJ90)
This string can be obtained from system properties like so:
String userAgent = System.getProperty( "http.agent" );
Solution 2
Starting from API level 17 there is a static method in WebSettings
which returns the default User-Agent string used by a WebView:
WebSettings.getDefaultUserAgent(context)
Since the method is static you don't need a WebView instance to run it.
Related videos on Youtube
cottonBallPaws
Updated on April 06, 2020Comments
-
cottonBallPaws about 4 years
I want to use the default user agent for the phone in a HttpClient connection and would like to know if there is a way to obtain the user agent without having to have a WebView to query.
-
Idolon almost 12 yearspossible duplicate of Retrieve User-Agent programatically
-
theblang about 10 years21 to 0, maybe you should reevaluate the accepted answer?
-