【问题标题】:Orientation is not Updating After unlock the Device in react-native- android在 react-native-android 中解锁设备后,方向未更新
【发布时间】:2017-09-05 13:07:38
【问题描述】:

我正在开发一个应用程序,实际上我的应用程序在选项卡/iPad 中显示横向和纵向。但是在平板电脑(iPad 工作正常)中,当我检查方向功能正常工作直到解锁设备时。当我将屏幕锁定在特定模式(如(纵向/横向))之后,设备在方向之前显示。不更新当前方向。

我点击了这个链接:https://github.com/yamill/react-native-orientation

这是我的代码:

componentWillMount(){
this.getOrientationtype()
}

getOrientationtype(){
    //alert("Hello")
    if(Platform.OS == 'ios'){  // to Identifying Android or iOS
     if(aspectRatio>1.6){  // Code for Iphone
       // alert("phone")
        Orientation.lockToPortrait()
       }
       else{
         Orientation.getOrientation((err, initial) => {
          if(initial != 'UNKNOWN'){
            this.setState({
              orientation:initial
            }) 
          }
          else{
            this.setState({
              orientation:'PORTRAIT'
            })
          }
        });

       }
     }
     else{

     if(DeviceInfo.isTablet()){
         // alert("android tab")
         Orientation.getOrientation((err, initial) => {
          if(initial != 'UNKNOWN'){
            this.setState({
              orientation:initial
            }) 
          }
          else{
            this.setState({
              orientation:'PORTRAIT'
            })
          }
        });
      }
      else{
        Orientation.lockToPortrait()
      }
     }
  }

请找出这个解决方案....我正在使用这个链接enter link description here

【问题讨论】:

    标签: react-native react-native-android


    【解决方案1】:

    1.你应该使用Orientation.addOrientationListener来收听Orientation Events

    2.从OrientationModule.java的源码看,这个库只是在onHostPause中调用了unregisterReceiver,所以锁屏后收不到onConfigurationChanged事件。一种方法是编辑@ 987654329@里面OrientationModule.java满足你想要的。

    @Override
    public void onHostResume() {
        final Activity activity = getCurrentActivity();
    
        if (activity == null) {
            FLog.e(ReactConstants.TAG, "no activity to register receiver");
            return;
        }
        activity.registerReceiver(receiver, new IntentFilter("onConfigurationChanged"));
        //add below code to onHostResume function
        //send broadcast onResume
        final int orientationInt = getReactApplicationContext().getResources().getConfiguration().orientation;
        Configuration newConfig = new Configuration();
        newConfig.orientation = orientationInt;
        Intent intent = new Intent("onConfigurationChanged");
        intent.putExtra("newConfig", newConfig);
        activity.sendBroadcast(intent);
    }
    

    完整的代码可以在这里找到OrientationModule.java

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多