HMS-Core / hms-react-native-plugin

This repo contains all of React-Native HMS plugins.
https://developer.huawei.com/consumer/en/doc/overview/HMS-Core-Plugin?ha_source=hms1
Apache License 2.0
245 stars 68 forks source link

react-native-hms-scan 有没有办法监听点击返回按钮 #47

Closed leoxiaoping closed 3 years ago

leoxiaoping commented 3 years ago

react-native-hms-scan扫码库识别率很高,如标题,有没有办法监听 startDefaultView() 的返回按钮

mustafahaluk commented 3 years ago

Unfortunately, there is no way to understand whether the user click the return button without changing java code of the plugin. This issue will be fixed in the next release. If you are able to change the plugin code, I can provide a quick fix here:

Replace lines from 307 to 324 in your_project/node_modules/@hmscore/react-native-hms-scan/android/src/main/java/com/huawei/hms/rn/scan/scanutils/RNHMSScanUtilsModule.java file with the code sample below:

@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_SCAN_DEFAULT && mPromise != null) {
        HMSLogger.getInstance(mReactContext).sendSingleEvent("ScanUtilsMethodCallHandler.defaultView");
        if (resultCode == RESULT_OK) {
            if (data != null) {
                HmsScan obj = data.getParcelableExtra(ScanUtil.RESULT);
                //Sending Result
                mPromise.resolve(toWM(gson.toJson(obj)));
            } else {
                mPromise.reject("NULL", "Data is null"); 
            }
        } else {
            mPromise.reject("NOT_OK", "Result is not ok"); // The promise will be rejected if user clicked the return button
        }
        mPromise = null;
    }
}
leoxiaoping commented 3 years ago

@mustafahaluk Thanks