如何使用sdk创建cordova插件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用sdk创建cordova插件相关的知识,希望对你有一定的参考价值。
我有一个离子3应用程序,我需要添加一个android sdk。没有插件,所以我必须为它创建一个cordova插件。我现在尝试了几天,我可以获得安装和构建的插件,没有错误,但调用插件方法似乎没有做任何事情。我可以获取插件的js文件来输出console.logs,但java文件似乎根本没有运行。这是我项目的离子信息。我对java代码或cordova插件开发没有经验,所以我遇到了一个主要的障碍,因为我无法弄清楚为什么没有调用java代码。谁能看到我做错了什么?离子型:
离子(离子CLI):4.4.0(/ usr / local / lib / node_modules / ionic)离子框架:离子角度3.9.2 @ ionic / app-scripts:3.2.0
科尔多瓦:
cordova(Cordova CLI):8.1.2(cordova-lib@8.1.1)Cordova平台:android 7.1.4,ios 4.5.5 Cordova插件:cordova-plugin-ionic-webview 1.2.1,(和其他31个插件)
系统:
ios-deploy:1.9.4 NodeJS:v10.7.0(/ usr / local / bin / node)npm:6.4.1 OS:macOS High Sierra Xcode:Xcode 10.1 Build版本10B61
这是我的Plugin.xml
<?xml version='1.0' encoding='utf-8'?>
<plugin id="cordova-plugin-chirp"
version="0.1.0"
xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android">
<name>chirp</name>
<js-module name="chirp" src="www/chirp.js">
<clobbers target="cordova.plugins.chirp" />
</js-module>
<platform name="ios">
<config-file parent="/*" target="config.xml">
<feature name="chirp">
<param name="ios-package" value="chirp" />
</feature>
</config-file>
<header-file src="src/ios/chirp.h" />
<source-file src="src/ios/chirp.m" />
</platform>
<platform name="android">
<source-file src="src/android/chirp.java" target-dir="src/org/apache/cordova/plugin/chirp" />
<source-file framework="true" src="src/android/libs/chirp-connect-release.aar" target-dir="libs"/>
<!-- <framework src="src/android/chirp.gradle" custom="true" type="gradleReference" /> -->
<config-file parent="/*" target="res/xml/config.xml">
<feature name="chirp">
<param name="android-package" value="org.apache.cordova.plugin.chirp" />
</feature>
</config-file>
<config-file parent="/*" target="AndroidManifest.xml">
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</config-file>
</platform>
</plugin>
这是我的js文件。 chirp.js:
var exec = require('cordova/exec');
var chirp ={
getPermissions:function(message){
console.log(message);
exec(null, null, 'Chirp', 'getPermissions', [message]);
},
start:function(message){
console.log(message);
exec(null, null, 'Chirp', 'start', [message]);
},
send:function(message){
console.log(message);
exec(null, null, 'Chirp', 'send', [message]);
}
};
module.exports = chirp
这是我的gradle文件。 chirp.gradle:
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
dependencies {
compile(name:'chirp-connect-release', ext:'aar')
}
android {
packagingOptions {
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
}
}
这是我的java文件。 chirp.java:
package com.chirp.cordova.plugin;
import android.content.Context;
import android.content.pm.PackageManager;
import android.Manifest;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.LOG;
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.PluginResult;
import org.apache.cordova.PermissionHelper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import io.chirp.connect.ChirpConnect;
import io.chirp.connect.interfaces.ConnectEventListener;
import io.chirp.connect.interfaces.ConnectSetConfigListener;
import io.chirp.connect.models.ChirpError;
import io.chirp.connect.models.ConnectState;
/**
* This class echoes a string called from javascript.
*/
public class chirp extends CordovaPlugin {
private ChirpConnect chirpConnect;
private Context context;
CallbackContext callbackC;
String TAG = "ChirpPlugin";
String [] permissions = {Manifest.permission.MODIFY_AUDIO_SETTINGS, Manifest.permission.RECORD_AUDIO, Manifest.permission.INTERNET, Manifest.permission.ACCESS_NETWORK_STATE};
public static final int SEARCH_REQ_CODE = 0;
public static final int SAVE_REQ_CODE = 1;
public static final int REMOVE_REQ_CODE = 2;
public static final int PICK_REQ_CODE = 3;
public String executeArgs = "";
String KEY = "myKey";
String SECRET = "mySecret";
String CONFIG = "myConfig";
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
callbackC = callbackContext;
LOG.d(TAG, "We are entering execute");
if (action.equals("start")){
if(hasPermission()){
String message = args.getString(0);
executeArgs = message;
// start(message);
callbackContext.success();
return true;
} else {
PermissionHelper.requestPermissions(this, 0, permissions);
}
} else if (action.equals("send")){
String message = args.getString(0);
this.send(message);
callbackContext.success();
return true;
}
return false;
};
public chirp(Context context) {
this.context = context;
};
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
ChirpConnect chirpConnect = new ChirpConnect(context, KEY, SECRET);
chirpConnect.setConfigFromNetwork(new ConnectSetConfigListener(){
@Override
public void onSuccess(){
chirpConnect.setListener(connectEventListener);
LOG.i("setConfig", "Config successfully set");
}
@Override
public void onError(ChirpError setConfigError){
LOG.e("setConfigError", setConfigError.getMessage());
}
});
};
ConnectEventListener connectEventListener = new ConnectEventListener() {
@Override
public void onSending(byte[] payload, byte channel) {
LOG.v("chirpConnectDemoApp", "This is called when a payload is being sent " + payload + " on channel: " + channel);
}
@Override
public void onSent(byte[] payload, byte channel) {
LOG.v("chirpConnectDemoApp", "This is called when a payload has been sent " + payload + " on channel: " + channel);
}
@Override
public void onReceiving(byte channel) {
LOG.v("chirpConnectDemoApp", "This is called when the SDK is expecting a payload to be received on channel: " + channel);
}
@Override
public void onReceived(byte[] payload, byte channel) {
LOG.v("chirpConnectDemoApp", "This is called when a payload has been received " + payload + " on channel: " + channel);
}
@Override
public void onStateChanged(byte oldState, byte newState) {
LOG.v("chirpConnectDemoApp", "This is called when the SDK state has changed " + oldState + " -> " + newState);
}
@Override
public void onSystemVolumeChanged(int old, int current) {
LOG.d("chirpConnectDemoApp", "This is called when the Android system volume has changed " + old + " -> " + current);
}
};
private void start(String message) {
if (message != null && message.length() > 0) {
chirpConnect.start();
}
};
private void send(String message) {
if (message != null && message.length() > 0) {
long maxLength = chirpConnect.getMaxPayloadLength();
byte[] payload = chirpConnect.randomPayload(maxLength);
chirpConnect.send(payload);
}
};
public boolean hasPermission() {
for(String p : permissions)
{
if(!PermissionHelper.hasPermission(this, p))
{
return false;
}
}
return true;
}
public void requestPermissions(int requestCode)
{
PermissionHelper.requestPermissions(this, requestCode, permissions);
}
}
我发现的一件事是你的插件Java文件的目标文件路径是错误的:
<source-file src="src/android/chirp.java" target-dir="src/org/apache/cordova/plugin/chirp" />
你的chirp
类的包名是com.chirp.cordova.plugin
所以target-dir
应该是src/org/apache/cordova/plugin
而不是src/org/apache/cordova/plugin/chirp
,即:
<source-file src="src/android/chirp.java" target-dir="src/org/apache/cordova/plugin" />
请注意,value
所需的<param name="android-package">
实际上是完全限定的类名(而不是包名),因此名称android-package
有点误导。因此,您目前拥有的是正确的:
<param name="android-package" value="org.apache.cordova.plugin.chirp" />
如果您正在开发Cordova插件,我建议您分别使用Android Studio和Xcode调试Android和iOS实现。
如果您在Android Studio中的测试工具应用程序中运行了插件,那么您会在logcat输出中看到一个警告,指示Cordova无法找到指定插件的本机实现。此外,您还可以使用Android Studio中的逐步调试器调试本机插件Java代码中的任何问题。
以上是关于如何使用sdk创建cordova插件的主要内容,如果未能解决你的问题,请参考以下文章
是否可以使用 Epson ePOS SDK for iOS 为 ionic 3 插件创建cordova?
如何将第三方框架添加到 Cordova 3.0 自定义插件?
Cordova 文件插件不适用于 android sdk 30