Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android user agent

I am writing an app in Android that uses a WebView to display HTML content. I was told to get an Android user agent for my app - how do I do that? I opened http://whatsmyuseragent.com from my app as well as the Android browser - both the user agents are the same.

Please help!

like image 612
Sagar Hatekar Avatar asked Apr 07 '11 19:04

Sagar Hatekar


People also ask

What is the Android User-Agent?

Published: 15 May 2022. The User-Agent (UA) string is contained in the HTTP headers and is intended to identify devices requesting online content. The User-Agent tells the server what the visiting device is (among many other things) and this information can be used to determine what content to return.

How do I find the User-Agent on Android?

agent, which can be used to retrieve the User-Agent string. String userAgent = System. getProperty("http. agent");

What is a User-Agent device?

User agent is a string of data from a user's device that represents data points like OS, browser, carrier, and hardware.

How do I change my User-Agent on my phone?

Switch to the “Advanced” tab in the top-right corner, then tap “User agent” at the top of the “Customize” sub-section. Tap “User agent” at the top of the “Customize” sub-section of the “Advanced” tab. Select one of the four built-in user agents or tap “Custom” and enter your own value, then tap “OK” to save.


4 Answers

After much research, I figured it out. There is a way to set a user agent for Android WebView.

webview.getSettings().setUserAgentString("user-agent-string");

http://developer.android.com/reference/android/webkit/WebSettings.html

like image 107
Sagar Hatekar Avatar answered Oct 14 '22 11:10

Sagar Hatekar


Put this in the onCreate method of the java class for the activity that displays the WebView:

WebView myWebView = (WebView)findViewById(R.id.webview);
//get the UA of the current running device:
String userAgent = view.getSettings().getUserAgentString() ;
//set the UA of the webview to this value:
myWebView.getSettings().setUserAgentString(userAgent);

Don't use System.getProperty("http.agent") - this will return the 'Dalvik' user agent (Dalvik is the VM that individual Android apps run within)

like image 20
Chris Halcrow Avatar answered Oct 14 '22 11:10

Chris Halcrow


You can't currently set the user-agent for WebView.

Update - I stand corrected!

In WebSettings there is a method called setUserAgentString:

webView.getSettings().setUserAgentString("my-user-agent");
like image 33
Matthew Willis Avatar answered Oct 14 '22 11:10

Matthew Willis


You can use System.getProperty("http.agent") to get the default device UA. And the webView.getSettings().getUserAgentString(); will give you the UA of the WebView. Be aware that we can set the UA programmatically. So it might not be the default device UA in all the cases.

System.getProperty("http.agent") is the better way to get the UA and can be retrieved before an instance of WebView is available.

like image 30
Sripathi Avatar answered Oct 14 '22 10:10

Sripathi