从Cordova 5.0.0访问appView

时间:2015-05-01 18:20:56

标签: java android cordova

我从最新的Android版Cordova版本(5.0.0)访问appView时遇到了麻烦。

例如,假设我想在我的应用中添加Javascript界面​​。在此版本之前,我曾经写过这行代码:

super.appView.addJavascriptInterface(new WebAppInterface(this), "jsInterface");

然后是WebAppInterface

public class WebAppInterface { ... }

现在,它只是不起作用。 Cordova最近有变化吗?我真的不知道该怎么做。

在这两种情况下(先前版本和新版本),我的主要活动都具有以下结构:

public class CordovaApp extends CordovaActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.init();
        loadUrl(Config.getStartUrl());
        ...
}

2 个答案:

答案 0 :(得分:19)

经过几天寻找解决方案后,我终于让应用程序正常运行。

Cordova已经改变了访问Android public class project2 { private static bitMap map; private static boolean hasDimension; private static boolean hasDefaultCell; public project2() { hasDimension = false; hasDefaultCell = false; } public static void main(String args[]) { try(Scanner parser = new Scanner(new File(args[0]))) { String commandToken = parser.nextLine(); System.out.println("Line: " + commandToken); while (commandToken.charAt(0) != 'q' ) { Scanner sc = new Scanner(commandToken); String commandLine = sc.next(); System.out.println("command:" + commandLine); if(commandLine.equals("d") && !hasDimension) { System.out.println("checkPoint! " + commandToken); setDimension(commandToken); hasDimension = true; } else if(commandLine.equals("b") && !hasDefaultCell) { System.out.println("checkPoint! " + commandToken); setDefaultCell(commandToken); hasDefaultCell = true; } else if(hasDimension && hasDefaultCell) { char command = commandToken.charAt(0); System.out.println("checkPoint! " + commandToken); switch(command) { case 'e': erase(commandToken); break; case 'x': ANSI.cls(); break; default: processShape(commandToken); break; } } commandToken = parser.nextLine(); } } catch(ArrayIndexOutOfBoundsException e) { if(e.getMessage().equals("p")) { System.out.println( "\nPlot - ArrayIndexOutOfBoundsException\n\n" ); } else { System.out.println( "\nError reading input file: " + args[0] ); e.printStackTrace(); } } catch(IOException e) { System.out.println( "\nError reading input file(from main): " + args[0]); e.printStackTrace(); } catch ( Exception e ) { throw e; } ANSI.cls(); map.printMap(); System.out.println("Dimensions(map): " + map.getWidth() + " x " + map.getHeight()); } private static void processShape(String command) { Scanner parser = new Scanner(command); String name = parser.next(); int x = parser.nextInt(); int y = parser.nextInt(); String o = parser.next(); parser.close(); System.out.println("Shape Name: "+ name); System.out.println("Location: " + x + " , " + y); System.out.println("Dimensions(Map > pre-shape-proc): " + map.getWidth() + " x " + map.getHeight()); bitMap b = new bitMap(map.getWidth(),map.getHeight()); System.out.println("Dimensions(Map > pre-shape-proc): " + b.getWidth() + " x " + b.getHeight()); shape s = new shape(name); System.out.println("Dimensions(map > after-shape-creation): " + map.getWidth() + " x " + map.getHeight()); System.out.println("Dimensions(Map > after): " + b.getWidth() + " x " + b.getHeight()); System.out.println("Shape Dimension: " + s.getWidth() + " x " + s.getHeight()); map.addShape(x,y,s); } private static void setDefaultCell(String command) { Scanner parser = new Scanner(command); parser.next(); String token = parser.next(); char c; if(token.equals("space")) { c = ' '; } else { c = token.charAt(0); } int fg = ANSI.color2Int(parser.next()); int bg = ANSI.color2Int(parser.next()); parser.close(); pixelCell cell = new pixelCell(c,fg,bg); map.setDefaultCell(cell); map.clearMap(map.getDefaultCell()); System.out.println("Default Cell: " + map.getDefaultCell()); } private static void setDimension(String command) { Scanner parser = new Scanner(command); System.out.println("command: " + command); parser.next(); int w = parser.nextInt(); int h = parser.nextInt(); map = new bitMap(w,h); System.out.println("Dimensions: " + map.getWidth() + " x " + map.getHeight()); } public static void erase(String command) { Scanner parser = new Scanner(command); parser.next(); char c = parser.next().charAt(0); int fg = ANSI.color2Int(parser.next()); int bg = ANSI.color2Int(parser.next()); parser.close(); pixelCell cell = new pixelCell(c,fg,bg); map.clearMap(cell); } } 的方式。使用Cordova 5.0.0及更新版本的开发人员需要将此行添加到其主要活动中:

webView

然后,像往常一样打电话给wV。例如,要添加Javascript接口:

WebView wV = (WebView)appView.getEngine().getView();

我希望这个答案可以帮助那些对此新更新感到困惑的其他人。

答案 1 :(得分:5)

在我找到你的答案之前我就要放弃了,这帮助我解决了一个相关的问题 - 感谢josemmo。

也许这会对别人有所帮助: 更新到Cordova 5 / Android 4后,我无法触发我的WebViewClient的shouldOverrideUrlLoading-Method,因为在新创建的WebView上设置WebViewClient

WebView webView = new WebView(this);
webView.setWebViewClient(new WebViewClient());
创建方法中的

没有做任何事情。

因此,解决方案是不创建新的WebView,而是使用类似的铸造appView AND引擎:

SystemWebViewEngine systemWebViewEngine = (SystemWebViewEngine) appView.getEngine();
WebViewClient myWebViewClient = new myWebViewClient(systemWebViewEngine);

WebView webView = (WebView) systemWebViewEngine.getView();
webView.setWebViewClient(myWebViewClient);

自定义WebViewClient类需要一个构造函数:

public class myWebViewClient extends SystemWebViewClient {

    public myWebViewClient(SystemWebViewEngine systemWebViewEngine) {
        super(systemWebViewEngine);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        ...
    }
}

我有点怀疑这应该怎么做,但至少它的工作。