I m trying to use native modules on react native to get the device name and package installer from android , but I got always a null value ?
my code like the following
the result always is null !
// 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);
}
}
and packages are :
// 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;
}
}
in the Main Application class I added the package
protected List<ReactPackage> getPackages() {
List<ReactPackage> packages = new PackageList(this).getPackages();
packages.add(new CxsNativeUtilsPackage());
packages.add(new ReactNativePackages());
return packages;
}
in my app I have the following code
const DeviceInfo = NativeModules.DeviceInfoModule;
DeviceInfo.getDeviceInstaller((err: string, name: string) => {
if (err) {
console.log('error', err);
} else {
console.log('device name', name);
}
});
any help? , thank you.