User-Agent string in Activity


  1. satb

    satb New Member

    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.

    Advertisement
    :
  2. satb

    satb New Member

    Noone knows the answer?

  3. markb

    markb Well-Known Member

    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 (Text):
    1. public class AboutActivity extends Activity {
    2.     private WebView m_webview = null ;
    3.    
    4.     @Override
    5.     protected void onCreate(Bundle savedInstanceState) {
    6.         super.onCreate(savedInstanceState);
    7.         setContentView(R.layout.about) ;
    8.        
    9.         m_webview = (WebView) findViewById(R.id.aboutview);
    10.         m_webview.getSettings().setJavaScriptEnabled(false);
    11.         m_webview.loadUrl("file:///android_asset/about.html") ;
    12.  
    13.         String ua = m_webview.getSettings().getUserAgentString() ;
    14.         Log.i("AboutActivity", "UA = "+ua) ;
    15.     }
    16. }
    17.  
    And I get this result:

    Code (Text):
    1. 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
    2.  
    Best of luck finding a alternative solution, if that won't work for you.

    Regards,

    Mark
Loading...

Share This Page