Last active
September 26, 2019 14:54
-
-
Save Ray33/ec99a4021bd48dedffca90dd19052d87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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