我开发了一个Android PhoneGap插件。该插件已成功调用,但未调用回调。我不知道我错过了什么。
有没有人知道在没有调用回调时可能出现什么问题?
以下是我的代码:
var SharedPreferencePlugin = function() {};
SharedPreferencePlugin.prototype.getvalues = function(content, success, fail) {
return PhoneGap.exec(
function(args) {
console.log("success called from plugin's js file");
},
function(args) {
console.log("failure called from plugin's js file");
},
'SharedPreferencePlugin',
'getvalues',
[content]
);
};
SharedPreferencePlugin.prototype.update = function(itemName, success, fail) {
return PhoneGap.exec(
function(args) {
console.log("success called from plugin's js file");
},
function(args) {
console.log("failure called from plugin's js file");
},
'SharedPreferencePlugin',
'update',
[itemName]
);
};
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin('SharedPreferencePlugin', new SharedPreferencePlugin());
});
public class SharedPreferencePlugin extends Plugin{
public static final String GET_ACTION = "getvalues";
public static final String UPDATE_ACTION = "update";
static Context staticContext = MainActivity.staticContext;
SharedPreferences dataStorage = staticContext.getSharedPreferences(MainActivity.PREFS_NAME, 0);
public PluginResult execute(String action, JSONArray data, String callbackId)
{
Log.d("SharedPreferencePlugin", "Plugin Called with action: " + action);
PluginResult result = null;
if(action.equals(GET_ACTION))
{
Log.d("SharedPrferencePlugin", "inside if for 'getvalues'");
JSONArray savedData = getPreferences();
Log.d("SharedPreferencePlugin", "Data: " + savedData);
result = new PluginResult(Status.OK, savedData);
}
else if(action.equals(UPDATE_ACTION))
{
try
{
updateSharedPreferences(data.getJSONObject(0).getString("itemName"));
JSONObject jsonObject = new JSONObject();
jsonObject.put("status", "success");
result = new PluginResult(PluginResult.Status.OK, jsonObject);
}
catch(JSONException ex)
{
Log.d("SharedPreferencePlugin", "Got JSONException: " + ex.getMessage());
result = new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}
else
{
result = new PluginResult(PluginResult.Status.JSON_EXCEPTION);
Log.d("SharedPreferencePlugin", "Invalid action: " + action + " obtained.");
}
return result;
}
public void updateSharedPreferences(String itemName)
{
Log.d("SharedPreferencePlugin", "Inside updateSharedPreferences, value passed: " + itemName);
SharedPreferences tmpPreferenceReference = staticContext.getSharedPreferences(MainActivity.PREFS_NAME, 0);
SharedPreferences.Editor editor = tmpPreferenceReference.edit();
if(itemName.equals(tmpPreferenceReference.getString(MainActivity.NAME_ITEM1, "")))
{
Integer tmpInt = Integer.parseInt(tmpPreferenceReference.getString(MainActivity.QUANTITY_ITEM1, "0")) - 1;
editor.putString(MainActivity.QUANTITY_ITEM1, tmpInt.toString());
}
editor.commit();
}
protected JSONArray getPreferences()
{
ArrayList<String> arrItemNames = new ArrayList<String>();
ArrayList<String> arrItemQuantities = new ArrayList<String>();
arrItemNames.add(0, dataStorage.getString(MainActivity.NAME_ITEM1, ""));
arrItemNames.add(1, dataStorage.getString(MainActivity.NAME_ITEM2, ""));
arrItemQuantities.add(0, dataStorage.getString(MainActivity.QUANTITY_ITEM1, ""));
arrItemQuantities.add(0, dataStorage.getString(MainActivity.QUANTITY_ITEM2, ""));
//-------------------------------------------------------------------
ArrayList<ArrayList> tempArrayList = new ArrayList<ArrayList>();
tempArrayList.add(arrItemNames);
tempArrayList.add(arrItemQuantities);
JSONArray jsonData = new JSONArray(tempArrayList);
//-------------------------------------------------------------------
return jsonData;
}
}
function test()
{
console.log("Test called");
window.plugins.SharedPreferencePlugin.getvalues({},
function() { // Success function
console.log("success called");
},
function() { // Failure function
console.log('Share failed');
}
);
}
非常感谢任何帮助。
感谢。
答案 0 :(得分:1)
当你说它肯定在运行本机代码时,你怎么知道这个?您是否看到了Log.d,或者您是否正在设置断点并单步执行并查看返回结果;线正在执行?
另外,您使用的是哪个版本的手机屏幕?
答案 1 :(得分:1)
如果您使用的是PhoneGap 1.2,则应删除该行:
PluginManager.addService("SharedPreferencePlugin","com.devapps.mmvspinningwheel.SharedPreferencePlugin");
因为不需要。此外,您应该将PhoneGap.addConstructor()移动到.js文件的底部。
Dean没错,因为像HTC这样的设备,console.log无法正常运行,你测试的是哪个版本的Android?
答案 2 :(得分:1)
谢谢你们的答案。
由于问题没有得到解决,我重新创建了一个新的测试项目并逐步进行,以确保每一小段代码都正常工作并慢慢移动到插件的预期目标。
我终于使用了我创建的新项目,并从头开始再次编写插件。
感谢。