Android开发系列8 Cordova架构学习 定义plugin

官网文档:

http://cordova.apache.org/

核心java类

CordovaActivity:Cordova Activity入口,已实现PluginManager、WebView的相关初始化工作, 只需继承CordovaActivity实现自己的业务需求。

PluginManager: 插件管理

ExposedJsApi :javascript调用Native

NativeToJsMessageQueue:Native调用javascript,主要包括三种方式:loadUrl 、 轮询、反射WebViewCore执行js

MainActivity

package com.example.hello;

import android.os.Bundle;
import org.apache.cordova.*;

public class MainActivity extends CordovaActivity {
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        loadUrl(launchUrl);   //只需要调用loadUrl就可以启动cordova框架
    }
}

自定义plugin

最终完成的plugin,要放置到:
/plugins下

参照cordova-plugin-whitelist 新建一个

com-whr-plugin-hello

在项目新建包com.whr.plugin,加类HelloPlugin.java

package com.whr.plugin.hello;

import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;

import org.apache.cordova.CordovaPlugin;

public class HelloPlugin extends CordovaPlugin {
      public boolean execute(String action, JSONArray args, CallbackContext callbackContext) 
                throws JSONException {
            Activity activity = this.cordova.getActivity();
            if (action.equals("getExtra")) {
                Intent i = activity.getIntent();
                if (i.hasExtra(Intent.EXTRA_TEXT)) {
                    callbackContext.success(i.getStringExtra(Intent.EXTRA_TEXT));
                } else {
                    callbackContext.error("");
                }
                return true;
            }
            return false;
        }
}

修改/res/xml/config.xml,加入:

    <feature name="Hello">
        <param name="android-package" value="com.whr.plugin.HelloPlugin" />
    </feature>

拷贝plugin.xml到com-whr-plugin-hello

内容修改为:(其中iOS部分不改)

<?xml version="1.0" encoding="UTF-8"?>

<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="com-whr-plugin-hello" version="1.1.0">
    <name>hello</name>
    <description>whr hello Plugin</description>
    <license>Apache 2.0</license>
    <keywords>whr,hello,policy</keywords>

    <engines>
      <engine name="cordova-android" version=">=4.0.0-dev" />
    </engines>

    <platform name="android">
        <config-file target="res/xml/config.xml" parent="/*">
            <feature name="Whitelist" >
                <param name="android-package" value="com.whr.plugin.hello.WhitelistPlugin"/>
                <param name="onload" value="true" />
            </feature>
        </config-file>

        <source-file src="src/android/HelloPlugin.java" target-dir="src/com/whr/plugin/hello" />

        <js-module src="hello.js" name="hello">
            <runs />
        </js-module>
    </platform>


</plugin>

js文件 hello.js

if (!document.querySelector('meta[http-equiv=Content-Security-Policy]')) {
    var msg = 'No Content-Security-Policy meta tag found. Please add one when using the cordova-plugin-whitelist plugin.';
    console.error(msg);
    setInterval(function() {
        console.warn(msg);
    }, 10000);
}

你可能感兴趣的:(Android开发系列8 Cordova架构学习 定义plugin)