我试图使用本机模块来从android获取设备名和包安装程序,但是我总是得到一个空值吗?
我的代码如下所示
结果总是为空!
// DeviceInfoModule.java
package com.example;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactMethod;
import android.annotation.SuppressLint;
import android.os.Build;
import android.content.Context;
import android.os.AsyncTask;
public class DeviceInfoModule extends ReactContextBaseJavaModule {
public DeviceInfoModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "DeviceInfoModule";
}
@ReactMethod
public void getDeviceName(final Callback callback) {
getDeviceNameHandler(callback);
}
@ReactMethod
public void getDeviceInstaller(final Callback callback) {
getDevicePackager(callback);
}
private void getDeviceNameHandler(final Callback callback) {
@SuppressLint("StaticFieldLeak") AsyncTask<Void,Void,Void> myAsyncTask = new AsyncTask<Void,Void,Void>() {
@SuppressLint("StaticFieldLeak")
@Override
protected Void doInBackground(final Void ... params) {
String device_Name = Build.MANUFACTURER + "-" + Build.MODEL;
callback.invoke( device_Name, device_Name);
return null;
}
};
myAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
private void getDevicePackager(final Callback callback) {
@SuppressLint("StaticFieldLeak") AsyncTask<Void,Void,Void> myAsyncTask = new AsyncTask<Void,Void,Void>() {
@Override
protected Void doInBackground(final Void ... params) {
Context context = getReactApplicationContext();
String installer = context.getPackageManager().getInstallerPackageName(context.getPackageName());
callback.invoke(null, installer);
return null;
}
};
myAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}一揽子计划包括:
// ReactNativePackages.java
package com.example;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import com.carexs.DeviceInfoModule;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ReactNativePackages implements ReactPackage {
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
@Override
public List<NativeModule> createNativeModules(
ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new DeviceInfoModule(reactContext));
return modules;
}
}在主应用程序类中,我添加了包
protected List<ReactPackage> getPackages() {
List<ReactPackage> packages = new PackageList(this).getPackages();
packages.add(new CxsNativeUtilsPackage());
packages.add(new ReactNativePackages());
return packages;
}在我的应用程序中,我有以下代码
const DeviceInfo = NativeModules.DeviceInfoModule;
DeviceInfo.getDeviceInstaller((err: string, name: string) => {
if (err) {
console.log('error', err);
} else {
console.log('device name', name);
}
});有什么帮助吗?谢谢。
发布于 2022-03-10 15:12:57
我找到了一个解决方案,也许总有一天会有人需要它,我发现我创建的本地方法不起作用--也许是因为代码中的一些不正确的模块或其他问题--我不太确定,我已经将这些方法更改为新版本,例如:
@ReactMethod
getDevicePackager() // (renamed to getDeviceInstaller()) method in DeviceInfoModule is changed to the following
// will be
@ReactMethod(isBlockingSynchronousMethod = true)
public String getInstallerPackageNameSync() {
String packageName = getReactApplicationContext().getPackageName();
String installerPackageName = getReactApplicationContext().getPackageManager().getInstallerPackageName(packageName);
if (installerPackageName == null) {
return "unknown";
}
return installerPackageName;
}
@ReactMethod
public void getDeviceInstaller(Promise p) { p.resolve(getInstallerPackageNameSync()); }https://stackoverflow.com/questions/71425468
复制相似问题