Skip to content

Instantly share code, notes, and snippets.

@Ray33
Last active September 26, 2019 14:54
Show Gist options
  • Save Ray33/ec99a4021bd48dedffca90dd19052d87 to your computer and use it in GitHub Desktop.
Save Ray33/ec99a4021bd48dedffca90dd19052d87 to your computer and use it in GitHub Desktop.
public static String getDefaultUserAgentString(Context context) {
if (Build.VERSION.SDK_INT >= 17) {
return NewApiWrapper.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) {
try{
return new WebView(context).getSettings().getUserAgentString();
}catch (Exception e1){
//no WebView is installed
return System.getProperty("http.agent") ;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment