在我的应用程序中获取不包含网络视图的用户代理

发布于 2024-10-31 20:06:52 字数 259 浏览 2 评论 0原文

我可以要求 WebView.getWebSettings().getUserAgentString() 来获取用户 代理,但这对我的应用程序来说效果不太好,因为我需要 尽管我不需要,但首先实例化一个 WebView

是否有另一种方法可以在不使用 WebView.getSetting 的情况下访问用户代理,因为在我的应用程序中,我不需要 webView

请帮助我

I can ask WebView.getWebSettings().getUserAgentString() to get the user
agent, but this doesn't work all that well for my app as I need to
instantiate a WebView first even though I don't need.

Is there another way to get to the User Agent without using a WebView.getSetting, because in my application, I don't need a webView?

Help me please

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我的影子我的梦 2024-11-07 20:06:52

您可以初始化 Webview 然后销毁它,或者只是将用户代理硬编码为字符串(并在运行时语言中替换等)。

You either initialize a Webview and then destroy it, or just hardcode the user agent as a string (and replace at run-time language etc).

呆萌少年 2024-11-07 20:06:52

您可以通过查看源代码来了解它是如何确定的

http://www.google.com/codesearch/p?hl=en#uX1GffpyOZk/core/java/android/ webkit/WebSettings.java&q=getCurrentUserAgent&sa=N&cd=1&ct=rc&l=370

根据文档,如果没有 WebView,则无法获取 WebSettings 对象。

您需要它与手机发送的信息完全相同吗?如果没有,只需选择一个标准的 Android 用户代理(不特定于构建/版本)

You can see how it's determined by looking at the source

http://www.google.com/codesearch/p?hl=en#uX1GffpyOZk/core/java/android/webkit/WebSettings.java&q=getCurrentUserAgent&sa=N&cd=1&ct=rc&l=370

According to the documentation, you can't get a WebSettings object without a WebView.

Do you need it to be exactly the one that the phone would send? If not, just pick up a standard Android User Agent (not build/version specific)

小瓶盖 2024-11-07 20:06:52

用户代理的信息是从HTTP头中获取的,这取决于使用什么浏览器来启动WebView对象。因此,如果您只想获取用户代理字符串而不创建WebView,那么这是没有意义的。

更好的方法可能是创建一个 WebView 并将其可见性设置为 GONE。获取用户代理字符串后,将其销毁。

The information of user agent is obtained from the HTTP headers, which depends on what browser is used to initiate the WebView object. Therefore, it doesn't make sense if you only want to get the user agent string without creating a WebView.

A better way might be create a WebView and set its visibility to GONE. After getting the user agent string, destroy it.

雪若未夕 2024-11-07 20:06:52

来自android源代码。

public static String getDefaultUserAgent() {
    StringBuilder result = new StringBuilder(64);
    result.append("Dalvik/");
    result.append(System.getProperty("java.vm.version")); // such as 1.1.0
    result.append(" (Linux; U; Android ");

    String version = Build.VERSION.RELEASE; // "1.0" or "3.4b5"
    result.append(version.length() > 0 ? version : "1.0");

    // add the model for the release build
    if ("REL".equals(Build.VERSION.CODENAME)) {
        String model = Build.MODEL;
        if (model.length() > 0) {
            result.append("; ");
            result.append(model);
        }
    }
    String id = Build.ID; // "MASTER" or "M4-rc20"
    if (id.length() > 0) {
        result.append(" Build/");
        result.append(id);
    }
    result.append(")");
    return result.toString();
}   

From android Source code.

public static String getDefaultUserAgent() {
    StringBuilder result = new StringBuilder(64);
    result.append("Dalvik/");
    result.append(System.getProperty("java.vm.version")); // such as 1.1.0
    result.append(" (Linux; U; Android ");

    String version = Build.VERSION.RELEASE; // "1.0" or "3.4b5"
    result.append(version.length() > 0 ? version : "1.0");

    // add the model for the release build
    if ("REL".equals(Build.VERSION.CODENAME)) {
        String model = Build.MODEL;
        if (model.length() > 0) {
            result.append("; ");
            result.append(model);
        }
    }
    String id = Build.ID; // "MASTER" or "M4-rc20"
    if (id.length() > 0) {
        result.append(" Build/");
        result.append(id);
    }
    result.append(")");
    return result.toString();
}   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文