我尝试在 phonegap / cordova 3.0.0 上运行我的原生 android 插件,但我不工作,
来自涟漪的错误:未捕获的 ReferenceError:未定义火炬
来自 index.html 的调用
<button onclick="torch.shine(200);">dummy</button>
plugin.xml
<!-- android -->
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="Torch">
<param name="android-package" value="org.holzi.torch.Torch"/>
<param name="onload" value="true" />
</feature>
</config-file>
<js-module src="www/torch.js" name="Torch">
<clobbers target="torch" />
</js-module>
<source-file src="src/android/Torch.java" target-dir="src/org/holzi/torch" />
<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.CAMERA"/>
</config-file>
</platform>
插件 www 文件夹中的 torch.js
var exec = require('cordova/exec');
/* constructor */
function Torch() {}
Torch.shine = function() {
exec(
function(result){ alert('ok: '+reply); },
function(err){ alert('Error: '+err); }
, "Torch", "shine", ['200']);
}
var torch = new Torch();
module.exports = torch;
和 Torch.java
/*
*/
package org.holzi.torch;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Context;
import android.os.Vibrator;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
public class Torch extends CordovaPlugin {
Camera camera;
Camera.Parameters Parameters;
public Torch() {
}
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("shine")) {
this.shine(20);
}
else {
return false;
}
// Only alert and confirm are async.
callbackContext.success();
return true;
}
public void shine(int time) {
//Torch torch = (Torch) this.cordova.getActivity().getSystemService(Context.VIBRATOR_SERVICE);
//torch.shine(time);
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
}
}