Passing onActivityResult in Cordova

别等时光非礼了梦想. 提交于 2019-12-03 14:33:17

Turns out that all I needed to do was to run cordova.setActivityResultCallback (this); before cordova.getActivity().startActivityForResult (intent, REQUEST_CODE_PICK_ACCOUNT);.

Author has answered the question already but in case someone wants a simpler example, here is what worked for me...

public final int MY_OP = 11;
private CallbackContext callback = null;

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException 
{
    if (action.equals("SERVICE NAME")) 
    {
        String param = "";
        try
        {
            param = args.getString(0);
        }
        catch( Exception e )
        {
        }

        callback = callbackContext;

        cordova.setActivityResultCallback (this);

        Intent intent = new Intent();
        intent.setClassName("com.pkg.drs","com.pkg.drs.ActivityToCall");
        intent.putExtra("my_param", param);

        cordova.startActivityForResult (this, intent, MY_OP);
        return true;
    }

    return false;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if( requestCode == MY_OP )
    {
        if( resultCode == Activity.RESULT_OK && data.hasExtra("return_val") )
        {
            PluginResult result = new PluginResult(PluginResult.Status.OK, data.getStringExtra("return_val"));
            result.setKeepCallback(true);
            callback.sendPluginResult(result);
        }
        else
        {
            PluginResult result = new PluginResult(PluginResult.Status.ERROR, "no params returned successfully" );
            result.setKeepCallback(true);
            callback.sendPluginResult(result);
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!