更改人行横道13+的用户代理在科尔多瓦的WebView [英] Change the user agent of Crosswalk 13+ as webview in Cordova

查看:472
本文介绍了更改人行横道13+的用户代理在科尔多瓦的WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变作为对的WebView科尔多瓦人行横道的用户代理。我目前使用的插件科尔多瓦 - 插件 - 人行横道-的WebView。

I am trying to change the User Agent of Crosswalk used as webview for Cordova. I am currently using the plugin cordova-plugin-crosswalk-webview.

我可以用下面的code香草科尔多瓦完成用户代理的定制:

I am able to accomplish the customization of the user agent with vanilla Cordova with the following code:

import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends CordovaActivity
{   

    public WebSettings settings;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.init();

        settings = ((WebView) super.appView.getEngine().getView()).getSettings();
        String defaultUA = settings.getUserAgentString();
        String customUA = defaultUA+" OreeganoC1";
        settings.setUserAgentString(customUA);

        loadUrl(launchUrl);
    }
}

然而,当我在人行横道插件运行它的应用程序崩溃,由于这片code的。一切都完美的作品没有人行横道。
我使用的科尔多瓦5.2.0和人行横道13。

However, when i run the app with the Crosswalk plugin it crashes due to this piece of code. Everything works perfectly without Crosswalk. I am using Cordova 5.2.0 and Crosswalk 13.

任何提示?

推荐答案

我不知道这是否是preferred方法或没有,但这里是我做了(使用人行横道14 +):

I'm not sure if this is the preferred method or not, but here's what I did (using Crosswalk 14+):


  1. 我添加了一个自定义的preference我的config.xml:

  1. I added a custom preference to my config.xml:

< preference NAME =xwalkUserAgentVALUE =自定义UA/>

项目/平台/安卓/ src目录/组织/人行横道/发动机/ XWalkWebViewEngine.java ,我加了班里面以下code:

In Project/platforms/android/src/org/crosswalk/engine/XWalkWebViewEngine.java, I added the following code inside the class:

public static final String PREF_USER_AGENT = "xwalkUserAgent";
protected CordovaPreferences preferences;

在构造函数中,我存储在preferences:

In the constructor, I stored the preferences:

public XWalkWebViewEngine(Context context, CordovaPreferences preferences) {
    this.preferences = preferences;
    ...
}

最后,在initWebViewSettings()方法,我设置了用户代理:

Finally, in the initWebViewSettings() method, I set the User Agent:

private void initWebViewSettings() {
    webView.setVerticalScrollBarEnabled(false);
    String xwalkUserAgent = preferences.getString(PREF_USER_AGENT, "");
    webView.setUserAgentString(xwalkUserAgent);
}


现在,每当我需要改变用户代理,我可以从config.xml中做到这一点。

Now whenever I need to change the User Agent, I can do it from config.xml.

这篇关于更改人行横道13+的用户代理在科尔多瓦的WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆