react-native 利用android 原生方法检查gps有没有开启

定义一个类继承 ReactContextBaseJavaModule

public class GpsModule extends ReactContextBaseJavaModule {

private LocationManager lm;

private ReactContext mcontext;

private String TAG ="GPSmoudule";

private static final String SCHEME = "package";

public GpsModule(ReactApplicationContext reactContext){

super(reactContext);

mcontext=reactContext;

}

//中心方法

@ReactMethod

public void openGps(Callback cb){

lm = (LocationManager) mcontext.getSystemService(Context.LOCATION_SERVICE);

boolean ok = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);

if(!ok){

Intent intent =new Intent();

intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

mcontext.startActivity(intent);

cb.invoke(false);

return;

}

cb.invoke(true);

}

//对外的方法

@Override

public String getName() {

return "GpsModule";

}

}

定义一个类 实现 ReactPackage

public class GpsReactPackage implements ReactPackage {

@Override

public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {

List<NativeModule> modules = new ArrayList<>();

modules.add(new GpsModule(reactContext)); //引用上一个类

return modules;

}

@Override

public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {

return Collections.emptyList();

}

}

3 在java mainActivity mainApplication 中使用

4 js 方法调用

NativeModule.GpsModule. openGps((result)=

>{

//返回一个结果是否有打开

})

猜你喜欢

转载自blog.csdn.net/weixin_41069726/article/details/88607204