Android
1. 在android/app/src/main/java/com/<projectname>/
文件夹下创建 < 模块文件 > OpenSettingsModule.java
在 OpenSettingsModule.java 下添加如下代码
package com.<projectname>; /** 记得把<projectname>改为你的项目名称 */
import android.app.Activity;
import android.content.Intent;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
public class OpenSettingsModule extends ReactContextBaseJavaModule {
@Override
public String getName() {
/**
* return the string name of the NativeModule which represents this class in JavaScript
* In JS access this module through React.NativeModules.OpenSettings
*/
return "OpenSettings";
}
@ReactMethod
public void openNetworkSettings(Callback cb) {
Activity currentActivity = getCurrentActivity();
if (currentActivity == null) {
cb.invoke(false);
return;
}
try {
currentActivity.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
cb.invoke(true);
} catch (Exception e) {
cb.invoke(e.getMessage());
}
}
/* constructor */
public OpenSettingsModule(ReactApplicationContext reactContext) {
super(reactContext);
}
}
2. 在android/app/src/main/java/com/<projectname>/
文件夹下创建 < 包文件 > OpenSettingsPackage.java(注册我们的模块)
上面的模块功能可以通过调用openNetworkSettings函数打开android设置。现在我们需要注册这个模块。
在 OpenSettingsPackage.java 下添加如下代码
package com.<projectname>; /** 记得把<projectname>改为你的项目名称 */
import android.app.Activity;
import android.content.Intent;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
public class OpenSettingsModule extends ReactContextBaseJavaModule {
@Override
public String getName() {
/**
* return the string name of the NativeModule which represents this class in JavaScript
* In JS access this module through React.NativeModules.OpenSettings
*/
return "OpenSettings";
}
@ReactMethod
public void openNetworkSettings(Callback cb) {
Activity currentActivity = getCurrentActivity();
if (currentActivity == null) {
cb.invoke(false);
return;
}
try {
currentActivity.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
cb.invoke(true);
} catch (Exception e) {
cb.invoke(e.getMessage());
}
}
/* constructor */
public OpenSettingsModule(ReactApplicationContext reactContext) {
super(reactContext);
}
}
3. 把包提供到MainApplication.java
文件的getPackages
方法中
import com.<projectname>.OpenSettingsPackage; /** 自定义的包模块 */
...
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new OpenSettingsPackage() /* 在这里添加 */
);
}
到这里基本准备完成了,如果你直接像下面那样调用,你会意想不到一些东西
import React, { Component } from 'react'
import { NativeModules, Button } from 'react-native'
export default class App extends Component {
constructor(props) {
super(props)
}
openSettings () {
NativeModules.OpenSettings.openNetworkSettings(data => {
console.log('call back data', data)
})
}
render() {
return (
<View>
<Button
onPress={this.openSettings}
title="打开设置"
color="#841584"
/>
<View/>
)
}
}
这里当你点击按钮时,会看到红框,null is not an object
因为你少了一步,就是下面的操作。用 Android Studio
打开你根项目下的 android
文件夹,然后 Build
再然后 Rebuild Project
,将生成的 apk
包进行安装,再打开,就不会有问题啦。
最后 感谢 这位大佬的博客