Example usage for android.webkit WebSettings getDefaultUserAgent

List of usage examples for android.webkit WebSettings getDefaultUserAgent

Introduction

In this page you can find the example usage for android.webkit WebSettings getDefaultUserAgent.

Prototype

public static String getDefaultUserAgent(Context context) 

Source Link

Document

Returns the default User-Agent used by a WebView.

Usage

From source file:com.halzhang.android.apps.startupnews.snkit.SNApi.java

public SNApi(Context context) {
    mAsyncHttpClient = new AsyncHttpClient();
    mAsyncHttpClient.addHeader("Accept-Language", "zh-cn");
    mAsyncHttpClient.addHeader("Accept", "*/*");
    mAsyncHttpClient.addHeader("Cookie", SessionManager.getInstance(context).getCookieString());
    mAsyncHttpClient.setUserAgent(USER_AGENT);
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
        mAsyncHttpClient.setUserAgent(WebSettings.getDefaultUserAgent(context));
    } else {/*from   w w w .ja v a2  s .c o  m*/
        mAsyncHttpClient.setUserAgent(USER_AGENT);
    }
}

From source file:com.linkbubble.util.Util.java

/**
 * From http://stackoverflow.com/a/5261472/328679
 *//*from  w w w  . j  a  va2 s. com*/
public static String getDefaultUserAgentString(Context context) {
    if (Build.VERSION.SDK_INT >= 17) {
        return WebSettings.getDefaultUserAgent(context);
    }

    try {
        Constructor<WebSettings> constructor = WebSettings.class.getDeclaredConstructor(Context.class,
                WebView.class);
        constructor.setAccessible(true);
        try {
            WebSettings settings = constructor.newInstance(context, null);
            return settings.getUserAgentString();
        } finally {
            constructor.setAccessible(false);
        }
    } catch (Exception e) {
        return Config.sIsTablet ? Constant.USER_AGENT_CHROME_TABLET : Constant.USER_AGENT_CHROME_PHONE;
    }
}