2017-09-05 89 views
1

我正在開發一個應用程序,其實我的應用程序顯示橫向和標籤/ iPad中的肖像。但在平板電腦(iPad的工作正常),當我檢查方向功能工作正常,直到解鎖設備。當我鎖定屏幕像特定模式(肖像/風景)之後,該設備顯示方向前顯示。不更新目前的方向。方向未更新解鎖設備後在react-native- android

我跟着這個鏈接: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.You應該使用Orientation.addOrientationListener來收聽Orientation Events

2.As從OrientationModule.java源代碼中看到,該庫只需要調用unregisterReceiveronHostPause,所以鎖定後無法收到onConfigurationChanged事件screen.One方法是編輯內部OrientationModule.javaonHostResume滿足你想要什麼。

@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

+0

呀真的非常感謝........ – Lavaraju