Phonegap原生android插件

时间:2013-09-30 13:30:04

标签: javascript android plugins cordova native

我尝试在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);

    }
}

2 个答案:

答案 0 :(得分:1)

我解决了它,这里是代码,如果其他人有同样的问题:

带有javascript的索引

<!DOCTYPE html>
    <html>
      <head>
        <title>Notification Example</title>

        <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
        <script type="text/javascript" charset="utf-8">


        document.addEventListener("deviceready", onDeviceReady, false);

        // Cordova is ready
        function onDeviceReady() {
            // Empty
        }

        function shine(torchOn) {
             navigator.notification.shine(torchOn);
        }

       function alertTorchError(message) {
            alert(message);
        }

        </script>
      </head>
      <body>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p><a href="#" onclick="shine(true); return false;">AN</a></p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p><a href="#" onclick="shine(false); return false;">AUS</a></p>
      </body>
    </html>

带有exec的js文件

var exec = require('cordova/exec');
module.exports = {
        shine: function(turnOn) {
        exec(null, function(error) { alertTorchError(error); }, "Torch", "shine", [turnOn]);

    },

};

和java文件

package org.apache.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.hardware.Camera;
import android.hardware.Camera.Parameters;

import android.content.pm.PackageManager;


/**
 * This class provides access to the Torch on the device.
 */
public class Torch extends CordovaPlugin {


  Camera camera;
  Camera.Parameters Parameters;
  boolean hasFlash;


/*  Constructor */
    public Torch() {  }

    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

        if (action.equals("shine")) {

                hasFlash = this.cordova.getActivity().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

                if (!hasFlash) {  callbackContext.error("no torch found");  } 
                else { this.shine(args.getBoolean(0));

    }

        }
        else {          
         return false;
        }
        return true;
    }


    //--------------------------------------------------------------------------
    // LOCAL METHODS
    //--------------------------------------------------------------------------


    public void shine(boolean turnOn) {



        if (camera == null) {  camera = Camera.open(); }

        if (turnOn) {
            Parameters p = camera.getParameters();
        p.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(p);
        }
        else {
            Parameters p = camera.getParameters();
        p.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(p);
        }

    }
}

答案 1 :(得分:0)

我不知道我的回答是否可以帮助你,但我多次遇到这个错误。以下是我的代码一年前使用cordova1.7.0,这个错误不在你的java代码中。

   var Music = function() {};   
   Music.prototype.exec = function(action, args, successCallback,errorCallback) {
   if (typeof successCallback !== "function") {
    console.log("GetResource: successCallback is not a function");
    return;}

   console.log("JS function execute --OK");

   if (errorCallback && (typeof errorCallback !== "function")) {
    console.log("GetResource: errorCallback is not a function");
    return;}

   PhoneGap.exec(successCallback, errorCallback, "MusicPlugin", action, args);
   };

   navigator.music=window.music = new Music();

然后,在XML文件中注册插件,如下所示:

 --your plugin package--
 <plugin name="MusicPlugin" value="***.***.***.MusicPlugin"></plugin> 

最后,你应该在doucument的'deviceready'事件之后调用这个变量。

 document.addEventListener('deviceready',function());