[github phonegap-plugins]でいろいろなのが開発されていますが、インストールで失敗する。cordova-plugman をつかってインストールするんですが、[お笑いプログラマの技術メモ]の状況とおんなじ感じです。
プラグインの仕組みもよくわからないし、ネット上の資料も古い。
こりゃちゃんと理解して自分で設定した方がよさそうだと調べました。
まずネイティブプラグインでPhoneGapを拡張する(Android)をみてプラグインを作成してみた。
がCordova(Phonegap)2.9では動かない。
大体プログラムしょっぱなの
public class HelloPlugin extends Plugin;のPluginでエラー
public class HelloPlugin extends CordovaPlugin;とCordovaPlugin となっていないとだめ。
[Developing a Plugin on Android]をみてソースを書き直す
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import android.util.Log;
public class HelloPlugin extends CordovaPlugin {
public static final String NATIVE_ACTION_STRING="nativeAction";
public static final String SUCCESS_PARAMETER="success";
@Override
public boolean execute(String action, JSONArray data,
CallbackContext callbackContext)throws JSONException {
Log.d("HelloPlugin", "Hello, this is a native function called from PhoneGap/Cordova!");
//only perform the action if it is the one that should be invoked
if (NATIVE_ACTION_STRING.equals(action)) {
String resultType = null;
try {
resultType = data.getString(0);
}
catch (Exception ex) {
Log.d("HelloPlugin", ex.toString());
}
if (resultType.equals(SUCCESS_PARAMETER)) {
callbackContext.success("Yay, Success!!!");
return true;
}
else {
callbackContext.success("Oops, Error :(");
return true;
}
}
return false;
}
}
で動きました。
あと、2.9からconfig.xmlにpluginタグがデフォでなかった。
ここら辺は [3.0リリースに向け激動のPhoneGap Android のプラグインが動かない!?]が参考になりました。3.0になってまた変わりそうです。
しかし、プラグイン、何とか仕組みが分かったので、手動で設定、コード修正で何とかなりそうです。