我已经使用 VSCode 创建了 React Native 应用程序,现在,我必须使用微型 USB 主机集成读取器设备。
我已经安装了阅读器应用程序来测试 APK。基于网络,我可以通过我当前应用程序的意图调用阅读器应用程序。设备网络链接在这里。
目前,我尝试了通过意图调用本机应用程序的搜索教程。但是我被困在放置脚本的方式上。我应该创建新文件/将脚本粘贴到现有文件下面/替换现有文件./android/app/src/main/java/com/myAppName/MainApplication.java
以及如何调用和接收来自我的扫描仪类的输出./src/screens/scanScreens/mainScanner.js
以下是我所做的,但坚持下一步我应该做什么:-
我当前的 MainActivity.java [更新]
package com.myappname;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
public class MainActivity extends ReactActivity {
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
@Override
protected String getMainComponentName() {
return "myAppName";
}
@Override
protected ReactActivityDelegate createReactActivityDelegate() {
return new ReactActivityDelegate(this, getMainComponentName()) {
@Override
protected ReactRootView createRootView() {
return new RNGestureHandlerEnabledRootView(MainActivity.this);
}
};
}
}
public class ShowKadModule extends ReactContextBaseJavaModule {
@ReactMethod
public void showKadApp() {
final Activity activity = getCurrentActivity();
Intent intent = new Intent ("com.causalidea.cikad.showkad.READ_MYKAD");
Bundle extras = new Bundle ();
extras.putBoolean ("READ_PHOTO", false); // Do not read the photo
intent.putExtras (extras);
activity.startActivityForResult (intent, 0);
}
public ShowKadModule(ReactApplicationContext reactContext) {
super(reactContext);
// Add the listener for `onActivityResult`
reactContext.addActivityEventListener(mActivityEventListener);
}
@Override
public String getName() {
return "ShowKadModule";
}
@Override
public void onActivityResult(int requestCode, int resultCode, final Intent data) {
if (resultCode == Activity.RESULT_OK) {
extras = intent.getExtras ();
if (extras != null) {
extras.getString ("IC_NUMBER");
extras.getString ("NAME");
extras.getString ("GENDER");
extras.getString ("CITIZENSHIP");
extras.getString ("BIRTH_DATE");
extras.getString ("RACE");
extras.getString ("RELIGION");
extras.getString ("ADDRESS_1");
extras.getString ("ADDRESS_2");
extras.getString ("ADDRESS_3");
extras.getString ("POSTCODE");
extras.getString ("CITY");
extras.getString ("STATE");
photoByteArray = extras.getByteArray ("PHOTO");
if (photoByteArray != null) {
bitmap = BitmapFactory.decodeByteArray (photoByteArray, 0, photoByteArray.length);
imageView.setImageBitmap (bitmap);
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
}
}
}
在经历了这么多示例和教程之后,我找到了一个简单的解决方案。通过从这里安装 react-native-activity-result 。
我如何执行此操作如下:
import ActivityResult from 'react-native-activity-result';
_onLaunch = async () => {
const activity = await ActivityResult.resolveActivity('com.causalidea.cikad.showkad.READ_MYKAD');
if (!activity) {
console.warn('Please install the othe app.');
ToastAndroid.show(`MyKAD application reader is not install`, ToastAndroid.SHORT, ToastAndroid.BOTTOM);
} else {
console.log(`Activity will be handled by ${activity.package}`);
ToastAndroid.show(`Activity will be handled by ${activity.package}`, ToastAndroid.SHORT, ToastAndroid.BOTTOM);
}
// Start an activity for a result
let uniqueId = 0;
let args = {READ_PHOTO: false};
ToastAndroid.show(`Test`, ToastAndroid.SHORT, ToastAndroid.BOTTOM);
const response = await ActivityResult.startActivityForResult(uniqueId, 'com.causalidea.cikad.showkad.READ_MYKAD', args);
ToastAndroid.show(`Result code: ${response.resultCode}`, ToastAndroid.LONG, ToastAndroid.BOTTOM);
if (response.resultCode !== ActivityResult.OK) {
ToastAndroid.show(`Invalid result from activity.`, ToastAndroid.SHORT, ToastAndroid.BOTTOM);
throw new Error('Invalid result from activity.');
} else {
console.log('Got the following response: ' + response.data);
ToastAndroid.show(`Response:\n${JSON.stringify(response)}`, ToastAndroid.SHORT, ToastAndroid.BOTTOM);
}
// Finish an activity with a result. This will close the caller too.
//ActivityResult.finish(ActivityResult.OK, 'com.causalidea.cikad.showkad.READ_MYKAD', args);
}
我可以启动另一个应用程序并等待活动结果,它会相应地进行操作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句