Is there any way to exactly implement a "desktop mode" function into my mobile app when loading website content?

huangapple go评论82阅读模式
英文:

Is there any way to exactly implement a "desktop mode" function into my mobile app when loading website content?

问题

有没有办法在加载网站内容时将“桌面模式”功能精确地实现到我的移动应用程序中?我正在制作一个Android应用程序,我想要一个页面,只包含静态网站内容,就像在Web浏览器中一样。我非常喜欢移动Opera实现了这一功能,所以我只想知道是否有方法可以实现这个功能。如果有的话,该如何实现?

(我正在使用Java制作这个项目,并使用来自“WebKit”库的WebView查看网站。)

我还尝试过更改用户代理,但在静态网站上没有效果。

英文:

Is there any way to exactly implement a "desktop mode" function into my mobile app when loading website content? I'm making a android app, and I want to have a page just with static website content just like in a web browser, I really like how mobile opera implemented this feature, So I just want to know if there is a way to do that. And if there is, then How?

(I'm making this project in Java and viewing the website using WebView from the library "WebKit")

I've also tried changing the User Agent, which didn't work on a static website.

答案1

得分: 1

以下是翻译好的部分:

这个方法帮助你在 WebView 上设置桌面模式

    public void setDesktopMode(WebView webView, boolean enabled) {
        String newUserAgent = webView.getSettings().getUserAgentString();
        if (enabled) {
            newUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5)\nAppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/85\nVersion/11.1.1 Safari/605.1.15";
        }

        webView.getSettings().setUserAgentString(newUserAgent);
        webView.getSettings().setUseWideViewPort(enabled);
        webView.getSettings().setLoadWithOverviewMode(enabled);
        webView.reload();
    }

像这样调用它

移动模式:`setDesktopMode(webView, false);`

桌面模式:`setDesktopMode(webView, true);`

对于 Kotlin:

fun setDesktopMode(webView: WebView, enabled: Boolean) {
    var newUserAgent: String? = webView.settings.userAgentString
    if (enabled) {
        newUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5)\nAppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/85\nVersion/11.1.1 Safari/605.1.15";
    }
    webView.settings.apply {
        userAgentString = newUserAgent
        useWideViewPort = enabled
        loadWithOverviewMode = enabled
    }
    webView.reload()
}
英文:

This method helps you to set DesktopMode on webview

public void setDesktopMode(WebView webView,boolean enabled) {
    String newUserAgent = webView.getSettings().getUserAgentString();
    if (enabled) {
        newUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5)\nAppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/85\nVersion/11.1.1 Safari/605.1.15";
    }

    webView.getSettings().setUserAgentString(newUserAgent);
    webView.getSettings().setUseWideViewPort(enabled);
    webView.getSettings().setLoadWithOverviewMode(enabled);
    webView.reload();
}

Call it like that

Mobile mode : setDesktopMode(webView, false);

Desktop mode : setDesktopMode(webView, true);

For Kotlin:

fun setDesktopMode(webView: WebView, enabled: Boolean) {
    var newUserAgent: String? = webView.settings.userAgentString
    if (enabled) {
        newUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5)\nAppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/85\nVersion/11.1.1 Safari/605.1.15"
    }
    webView.settings.apply {
        userAgentString = newUserAgent
        useWideViewPort = enabled
        loadWithOverviewMode = enabled
    }
    webView.reload()
}

huangapple
  • 本文由 发表于 2023年2月7日 04:36:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定