首页 文章

方向不是更新在react-native-android中解锁设备后

提问于
浏览
1

我正在开发一个应用程序,实际上我的应用程序在标签/ 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

1 回答

  • 1

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

    2.从OrientationModule.java的源代码中可以看出,这个库只需在 onHostPause 中调用 unregisterReceiver ,因此在锁定屏幕后无法接收 onConfigurationChanged 事件 . 一种方法是编辑OrientationModule.java里面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

相关问题