sendPluginResult()在onActivityResult()中不起作用 - Cordova

时间:2016-01-11 09:31:15

标签: android cordova plugins

我是科尔多瓦的新手。我正在Android中为PhoneGap创建一个用于视频捕获的自定义插件。在我的onActivityResult()中,我收到了捕获的视频。我在onActivityResult()内添加了一个无法正常工作的回调。

这是我的代码:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        final Mediatest that = this;
        // TODO Auto-generated method stub
        if (resultCode == cordova.getActivity().RESULT_OK) {
            if (requestCode == REQUEST_VIDEO_CAPTURED) {
                uriVideo = data.getData();
                Toast.makeText(cordova.getActivity().getApplicationContext(),
                        uriVideo.getPath(), Toast.LENGTH_LONG).show();
                 that.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 0));


            }
        } else if (resultCode == cordova.getActivity().RESULT_CANCELED) {
            uriVideo = null;
            Toast.makeText(cordova.getActivity().getApplicationContext(),
                    "Cancelled!", Toast.LENGTH_LONG).show();
        }
    }

但是当我在onActivityResult之外添加回调方法时,它正在工作:

private void captureVideo(Long limit) {
        this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 0));
        Intent intent = new Intent(
                android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, limit);
        cordova.setActivityResultCallback (this);
        this.cordova.startActivityForResult((CordovaPlugin) this, intent, REQUEST_VIDEO_CAPTURED);
        this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 0));

    }

Toast

Toast.makeText(cordova.getActivity().getApplicationContext(),
                        uriVideo.getPath(), Toast.LENGTH_LONG).show();

正在显示,但回调无法正常工作。我无法追踪此问题。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

当您调用插件时,首先使用NO_RESULT发送插件结果并将KeepCallback设置为true

private void captureVideo(Long limit) {

    Intent intent = new Intent(
                android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, limit);
    cordova.setActivityResultCallback (this);
    this.cordova.startActivityForResult((CordovaPlugin) this, intent, REQUEST_VIDEO_CAPTURED);

    //Send a plugin result with NO_RESULT and set KeepCallback as true
    PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
    r.setKeepCallback(true);
    callbackContext.sendPluginResult(r);

}    

然后,您应该可以从sendPluginResult()

拨打onActivityResult()