React-native从startActivityForResult中获取结果

前端之家收集整理的这篇文章主要介绍了React-native从startActivityForResult中获取结果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

注:以下内容涉及原生的调用逻辑,有必要很清楚。

android端

在这部分发现官方文档的内容并不能调用到module中的activityResult方法,在整理了逻辑以后,觉得应该在包含reactView的activity中写回调,比如我新建了一个MyReactActivit,里边包含一个mReactRootView

public class MyReactActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler {
    protected ReactRootView mReactRootView;
    private ReactInstanceManager mReactInstanceManager;
    private static final int OVERLAY_PERMISSION_REQ_CODE = 233;
    private static final int IMAGE_PICKER_REQUEST = 12345;
    private static final String E_PICKER_CANCELLED = "E_PICKER_CANCELLED";
    private static final String E_NO_IMAGE_DATA_FOUND = "E_NO_IMAGE_DATA_FOUND";
    //构建一个阻塞的单一数据的队列
    public static ArrayBlockingQueue<String> mQueue = new ArrayBlockingQueue<String>(1);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent,OVERLAY_PERMISSION_REQ_CODE);
            }
        }
        mReactRootView = new ReactRootView(this);
        mReactInstanceManager = ReactInstanceManager.builder()
                .setApplication(getApplication())
                .setBundleAssetName("index.android.bundle")
                .setJSMainModuleName("index.android")
                .addPackage(new MainReactPackage())
                .addPackage(new CustomReactPackage())
                .setUseDeveloperSupport(BuildConfig.DEBUG)
                .setInitialLifecycleState(LifecycleState.RESUMED)
                .build();
        mReactRootView.startReactApplication(mReactInstanceManager,"HelloWorld",null);

        setContentView(mReactRootView);
    }

以上是MyReactActivity的部分内容,在里边重写onActivityResult方法,如下:

@Override
    protected void onActivityResult(int requestCode,int resultCode,Intent intent) {
        if (requestCode == IMAGE_PICKER_REQUEST) {
                if (resultCode == Activity.RESULT_CANCELED) {
                    mQueue.add(E_PICKER_CANCELLED+"Image picker was cancelled");
                } else if (resultCode == Activity.RESULT_OK) {
                    Uri uri = intent.getData();
                    if (uri == null) {
                        mQueue.add(E_NO_IMAGE_DATA_FOUND+"No image data found");
                    } else {
                        mQueue.add(uri.toString());
                    }
                }
        }
        if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (!Settings.canDrawOverlays(this)) {
                    // SYSTEM_ALERT_WINDOW permission not granted...
                }
            }
        }
    }

里边用mQueue来存储的回调数据。以上是回调部分,调用的写法如下:

在我的测试module中写了一个pickImage方法代码如下:

@ReactMethod
    public void pickImage(Callback successBack,Callback errorBack) {
        Activity currentActivity = getCurrentActivity();

        if (currentActivity == null) {
            errorBack.invoke("Activity doesn't exist");
            return;
        }

        try {
            final Intent galleryIntent = new Intent(Intent.ACTION_PICK);

            galleryIntent.setType("image/*");
            final Intent chooserIntent = Intent.createChooser(galleryIntent,"Pick an image");
            currentActivity.startActivityForResult(chooserIntent,IMAGE_PICKER_REQUEST);
            successBack.invoke(MyReactActivity.mQueue.take());
        } catch (Exception e) {
            errorBack.invoke(e.getMessage());
            e.printStackTrace();
        }
    }

以上调用了本机的选择图片功能。然后两个参数successBack和errorBack是js端传来的,用来返回数据到js。可以看到上面使用了MyReactActivity的静态变量mQueue用来获取回调到onActivityResult方法中的值,再返回到js中。

Js端的调用方式:

NativeModules.MyTestModule.pickImage((msg) => {
                    ToastAndroid.show('JS界面:从Activity中传输过来的数据为:'+msg,ToastAndroid.SHORT);
                  },(result) => {
                    ToastAndroid.show('JS界面:错误信息为:'+result,ToastAndroid.SHORT);
        });

以上就是从startActivityForResult中获取结果的所有方法,流程和官网的并不相同的,还不清楚官网的调用方式是否可行,但是经过测试并没有成功。当然我写的以上的方式成功了。

以上

原文链接:https://www.f2er.com/react/305030.html

猜你在找的React相关文章