我正在开发一个 phonegap 插件来启动一项活动。我使用“ startActivityForResult ”方法启动活动,但在活动结束之前触发了“ onActivityResult ”方法,因此我无法管理活动的结果。
这是插件的代码:
package com.phonegap.plugin.escanerDocumentos;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.visualtaggingservices.scandoc.ScanDocConstants;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
public class EscanerDocumentos extends Plugin {
private static final int SCANDOC_REQUEST_RESULT = 1;
public static final String PATH_ME="path_mem_ext";
public static final String PATH_MI="path_mem_int";
public String strFilename="inicializado";
private String callback=null ;
//public static final String COMPRESS="compress";
final static String TARGET_BASE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()+"/";
@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
this.callback = callbackId;
if (action.equalsIgnoreCase("EscanerDocumentos")) {
Escanea();
}
PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
r.setKeepCallback(true);
return r;
}
public EscanerDocumentos() {
}
private void Escanea() {
Uri uri = Uri.parse("scandoc://com.visualtaggingservices.scandoc");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.putExtra(ScanDocConstants.SCANDOC_KEY_PICTURE_WIDTH, ScanDocConstants.PICTURE_WIDTH);
intent.putExtra(ScanDocConstants.SCANDOC_KEY_PICTURE_HEIGHT, ScanDocConstants.PICTURE_HEIGHT);
this.cordova.startActivityForResult((Plugin)this,intent,SCANDOC_REQUEST_RESULT);
}
// ===================================================================
// ON_ACTIVITY_RESULT
// -------------------------------------------------------------------
/**
* Funcion que obtiene el resultado de la Actividad.
* @param requestCode Codigo de la actividad que finaliza.
* @param resultCode Resultado de la actividad que finaliza.
* @param data Datos de la actividad que finaliza.
*/
// ===================================================================
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.d("EscanerDocumentos", "onActivityResult");
if (requestCode == SCANDOC_REQUEST_RESULT)
{
if (resultCode == Activity.RESULT_OK)
{
Bundle bundle = data.getExtras();
strFilename = bundle.getString(ScanDocConstants.SCANDOC_KEY_FILENAME);
JSONObject obj = new JSONObject();
try {
obj.put("filename", bundle.getString(ScanDocConstants.SCANDOC_KEY_FILENAME));
obj.put("signed", bundle.getString(ScanDocConstants.SCANDOC_KEY_SIGNED));
}
catch(JSONException e) {
Log.e("EscanerDocumentos", "Error al devolver el resultado");
}
Log.d("EscanerDocumentos", "Devolvemos el pluginResult");
this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback);
}
}
}
}
谢谢你的帮助!!!