Kelichao / cordova

cordova研究集
2 stars 0 forks source link

cordova

打包教程

一、环境

  1. NodeJS
  2. NPM
  3. JDK安装与配置JDK下载地址 (要特别注意的是,java -version 这个命令需要在环境变量保存完毕后运行才能看到)
  4. Android SDK(找其中的SDK Tools)

二、创建目录

cordova create app #app为入口目录

三、添加平台

$ cd app

$ cordova platform add android

四、构建和运行

重要插件

h5页面调用本地插件接口

<!--<script src="https://github.com/Kelichao/cordova/raw/master/file:///android_asset/www/cordova.js" type="text/javascript" charset="UTF-8"></script> -->
<script src="http://injection/www/cordova.js" type="text/javascript" charset="UTF-8"></script>
    <!-- <script type="text/javascript" src="https://github.com/Kelichao/cordova/raw/master/js/index.js"></script>
    <script type="text/javascript" src="https://github.com/Kelichao/cordova/raw/master/cordova_plugins.js"></script> -->
public void clearAuthenticationTokens() {
    this.authenticationTokens.clear();
}

-- 新增开始 --
private static final String INJECTION_TOKEN = "http://injection/"; 
-- 新增结束 --

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {

    ---新增开始---
    if(url != null && url.contains(INJECTION_TOKEN)) {
        String assetPath = url.substring(url.indexOf(INJECTION_TOKEN) + INJECTION_TOKEN.length(), url.length());
        try {
            WebResourceResponse response = new WebResourceResponse(
                    "application/javascript",
                    "UTF8",
                    view.getContext().getAssets().open(assetPath)
            );
            return response;
        } catch (IOException e) {
            e.printStackTrace(); // Failed to load asset file
            return new WebResourceResponse("text/plain", "UTF-8", null);
        }
    }
    ---新增结束---
    try {

        // Check the against the whitelist and lock out access to the WebView directory
        // Changing this will cause problems for your application
        if (!parentEngine.pluginManager.shouldAllowRequest(url)) {
            LOG.w(TAG, "URL blocked by whitelist: " + url);
            // Results in a 404.
            return new WebResourceResponse("text/plain", "UTF-8", null);
        }

        CordovaResourceApi resourceApi = parentEngine.resourceApi;
        Uri origUri = Uri.parse(url);
        // Allow plugins to intercept WebView requests.
        Uri remappedUri = resourceApi.remapUri(origUri);

        if (!origUri.equals(remappedUri) || needsSpecialsInAssetUrlFix(origUri) || needsKitKatContentUrlFix(origUri)) {
            CordovaResourceApi.OpenForReadResult result = resourceApi.openForRead(remappedUri, true);
            return new WebResourceResponse(result.mimeType, "UTF-8", result.inputStream);
        }
        // If we don't need to special-case the request, let the browser load it.
        return null;
    } catch (IOException e) {
        if (!(e instanceof FileNotFoundException)) {
            LOG.e(TAG, "Error occurred while loading a file (returning a 404).", e);
        }
        // Results in a 404.
        return new WebResourceResponse("text/plain", "UTF-8", null);
    }
}