当前位置: 首页 > 知识库问答 >
问题:

为什么我得到null值,而不是字符串值总是当调用一个方法从android本地模块

郭炳
2023-03-14

我试图使用本机模块上的反应本机从android获取设备名称和软件包安装程序,但我总是得到一个空值?

我的代码如下

结果总是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);
  }

}


和包是:

// 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;
  }
}

在Main Application类中,我添加了包

 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);
          }
        });

有什么帮助吗,非常感谢。

共有1个答案

斜高翰
2023-03-14

我发现了一个解决方案,也许有一天有人会需要它,我发现我创建的反应本机方法不起作用,也许是因为一些废弃的模块或代码上的其他问题,我不确定,我已经将这些方法更改为新版本,例如:

@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()); }

 类似资料: