• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps User-Agent string in Activity

satb

Lurker
Apr 16, 2010
4
0
I have an activity inside which I want to get the User-Agent string which is available to WebKit. I need to transmit the user-agent to the server on every server request. I don't want to hard code any fixed user agent string in my application because we want to track which devices are being used with our application and the best way to do that would be to use what comes built in with the device (inside WebKit).

Is there any way I can get that user agent that WebKit uses inside my activity? Just FYI...I don't have any WebView. I just have a normal activity with some layouts and buttons. I couldn't find any documentation. Any ideas? Thanks in advance.
 
Noone knows the answer?

I have an activity inside which I want to get the User-Agent string which is available to WebKit. I need to transmit the user-agent to the server on every server request. I don't want to hard code any fixed user agent string in my application because we want to track which devices are being used with our application and the best way to do that would be to use what comes built in with the device (inside WebKit).

Is there any way I can get that user agent that WebKit uses inside my activity? Just FYI...I don't have any WebView. I just have a normal activity with some layouts and buttons. I couldn't find any documentation. Any ideas? Thanks in advance.
 
Upvote 0
The information you're after is easy to get if you have a WebView object.
I'm not aware of any other way to get it. Although to be honest I only spent 2 minutes looking. (I stopped when I found the WebView solution.)

The code below is from one of my apps where I use a WebView to show an about/help page. I added the last 2 lines to get the User Agent string.

Code:
public class AboutActivity extends Activity {
	private WebView m_webview = null ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.about) ;
		
		m_webview = (WebView) findViewById(R.id.aboutview);
		m_webview.getSettings().setJavaScriptEnabled(false);
		m_webview.loadUrl("file:///android_asset/about.html") ;

		String ua = m_webview.getSettings().getUserAgentString() ;
		Log.i("AboutActivity", "UA = "+ua) ;
	}
}

And I get this result:

Code:
UA = Mozilla/5.0 (Linux; U; Android 2.1; en-us; sdk Build/ERD79) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17

Best of luck finding a alternative solution, if that won't work for you.

Regards,

Mark
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones