我在Android Phonegap: Notify javascript when an AsyncTask is finished
上遇到像Peacemoon一样的问题嗯,这适用于较旧的PhoneGap版本,但...... 我的问题:这不适用于像PhoneGap 3.4这样的新PhoneGap版本。
插件类现在继承自CordovaPlugin,而execute方法的返回类型现在是boolean,参数是CallbackContext而不是int callbackId,依此类推。我现在如何实现异步任务?
我的方法:
private CallbackContext myCallbackContext;
@Override
public boolean execute(String action, JSONArray args,
final CallbackContext callbackId)
{
this.myCallbackContext = callbackId;
if (action.equals("test"))
{
Intent intent = new Intent("com.companyname.name.TEST");
this.cordova.startActivityForResult((CordovaPlugin) this, intent, 0);
// create an empty result, because the asynchronous call can take long
PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
pluginResult.setKeepCallback(true);
this.myCallbackContext.sendPluginResult(pluginResult);
this.myCallbackContext.success;
return true;
}
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if (requestCode == 0)
{
if (resultCode == Activity.RESULT_OK)
{
Bundle korb = intent.getExtras();
String osVersion = korb.getString("osVersion");
JSONObject obj = new JSONObject();
try
{
obj.put("osVersion", osVersion);
} catch(JSONException e)
{
// Log
}
PluginResult result = new PluginResult(PluginResult.Status.OK, obj);
result.setKeepCallback(false);
this.myCallbackContext.sendPluginResult(result);
this.myCallbackContext.success(obj);
}
我的JavaScript回归未定义。但它适用于旧版本的旧版PhoneGap,如1。它如何在较新的PhoneGap版本上运行?
答案 0 :(得分:1)
这是我的phonegap 3.x插件类的内容:
public CallbackContext callbackContext;
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
this.callbackContext = callbackContext;
if (action.equals("anAction")) {
Intent intent = new Intent(this.getActivity(),com.companyname.name.TEST.class);
if (this.cordova != null) {
this.cordova.startActivityForResult(this, intent, 0);
return true;
}
}
return false;
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case 0:
//retour de la version utilisant le sample de imense alpr
if(resultCode == android.app.Activity.RESULT_OK){ //0:ok
String result=intent.getStringExtra("result");
this.callbackContext.success(result);
}
else{
String message=intent.getStringExtra("result");
this.callbackContext.error(message);
}
break;
default:
break;
}
}
答案 1 :(得分:0)
自从Cordova / PhoneGap的新版本以来,我的MainActivity实现了CordovaInterface。因此有必要实现方法“startActivityForResult”。如果我调用“this.cordova.startActivityForResult((CordovaPlugin)this,intent,0)”,则在MainActivity上设置范围,并且新意图的调用将覆盖MainActivity中的方法“startActivityForResult”。因此,结果返回到MainActivity中的“onActivityResult”。在这里,我必须在插件中调用我的方法:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent){
Log.d("Debug", "-----> MainActivity: onActivityResult");
if(this.activityResultCallback != null)
{
// this is the solution :)
this.activityResultCallback.onActivityResult(requestCode, resultCode, intent);
}
}
此外,意图调用同时适用于:
Intent intent = new Intent(this.cordova.getActivity(), com.companyname.name.TestActivity.class);
if (this.cordova != null) {
this.cordova.startActivityForResult(this, intent, 0);
return true;
}
或
Intent intent = new Intent("com.companyname.name.TEST");
if (this.cordova != null) {
this.cordova.startActivityForResult((CordovaPlugin) this, intent, 0);
}
<!--you must add the String to the AndroidManifest.xml-->
<activity
android:name=".TestActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="de.companyname.name.TEST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
否则(QuickFix如何描述)在exute方法中我必须使用'return true'或'return false',如果发生错误。在onActivityResult方法中,我必须使用'myCallbackContext.success(obj);'。没有其他的。我不需要像'this.myCallbackContext.sendPluginResult(pluginResult);'。