有时候需要针对不同的Android版本做一些修整,因此获得当前使用手机的版本号至关重要。
1、初始化一个RN项目并用Android Studio打开Android部分
2、在MainActivity.java 文件中加入以下代码
public class MainActivity extends ReactActivity {
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
@Override
protected String getMainComponentName() {
return "StatusBarDemo";
}
//添加以下代码开始
@Override
protected ReactActivityDelegate createReactActivityDelegate() {
return new ReactActivityDelegate(this, getMainComponentName()) {
@Nullable
@Override
protected Bundle getLaunchOptions() {
Bundle bundle = new Bundle();
bundle.putInt("Android_SDK_INT", Build.VERSION.SDK_INT);
return bundle;
}
};
}
//添加以下代码结束
}
3、在项目的js文件中加入(一定要加入到生命周期constructor或其以后)
constructor(props){
super(props);
console.log("手机版本号",this.props.Android_SDK_INT)
}
此时this.props.Android_SDK_INT就是Android的API Level
4、下面表格是API Level与Android版本的对应关系。