获取手机方向,但将屏幕方向修改为纵向

我想获得手机的方向,但保持屏幕的方向肖像。 所以无论用户把手机变成横向还是纵向,视图都保持不变,但是我可以看到它是转向横向还是纵向。

将活动设置为android:screenOrientation =“portrait”将解决这两个问题,但是我无法通过手机方向检测

public void onConfigurationChanged(Configuration newConfig) { switch (newConfig.orientation) { case Configuration.ORIENTATION_PORTRAIT: Toast.makeText(this, "Portrait", Toast.LENGTH_SHORT).show(); break; case Configuration.ORIENTATION_LANDSCAPE: Toast.makeText(this, "Landscape", Toast.LENGTH_SHORT).show(); break; default: break; } } 

有没有人有一个想法如何解决这个问题?

Solutions Collecting From Web of "获取手机方向,但将屏幕方向修改为纵向"

你能用加速度计满足你的要求吗? 如果是这样,也许这样的(未经testing)片段将适合您的目的。

 SensorManager sensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE); sensorManager.registerListener(new SensorEventListener() { int orientation=-1;; @Override public void onSensorChanged(SensorEvent event) { if (event.values[1]<6.5 && event.values[1]>-6.5) { if (orientation!=1) { Log.d("Sensor", "Landscape"); } orientation=1; } else { if (orientation!=0) { Log.d("Sensor", "Portrait"); } orientation=0; } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } }, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME); 

这是一个多用途类,可以轻松pipe理屏幕方向更改:

 public class OrientationManager extends OrientationEventListener { public enum ScreenOrientation { REVERSED_LANDSCAPE, LANDSCAPE, PORTRAIT, REVERSED_PORTRAIT } public ScreenOrientation screenOrientation; private OrientationListener listener; public OrientationManager(Context context, int rate, OrientationListener listener) { super(context, rate); setListener(listener); } public OrientationManager(Context context, int rate) { super(context, rate); } public OrientationManager(Context context) { super(context); } @Override public void onOrientationChanged(int orientation) { if (orientation == -1){ return; } ScreenOrientation newOrientation; if (orientation >= 60 && orientation <= 140){ newOrientation = ScreenOrientation.REVERSED_LANDSCAPE; } else if (orientation >= 140 && orientation <= 220) { newOrientation = ScreenOrientation.REVERSED_PORTRAIT; } else if (orientation >= 220 && orientation <= 300) { newOrientation = ScreenOrientation.LANDSCAPE; } else { newOrientation = ScreenOrientation.PORTRAIT; } if(newOrientation != screenOrientation){ screenOrientation = newOrientation; if(listener != null){ listener.onOrientationChange(screenOrientation); } } } public void setListener(OrientationListener listener){ this.listener = listener; } public ScreenOrientation getScreenOrientation(){ return screenOrientation; } public interface OrientationListener { public void onOrientationChange(ScreenOrientation screenOrientation); } } 

这是更简单,可重用的方式,你也可以得到REVERSE_LANDSCAPE和REVERSE_PORTRAIT方向。

您必须实施OrientationListener才能在发生方向更改时得到通知。

不要忘了调用orientationManager.enable()来开始方向跟踪,然后调用orientationManager.disable()(这两个方法是从OrientationEventListener类inheritance的)

更新:用例的例子

 MyFragment extends Fragment implements OrientationListener { ... @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); orientationManager = new OrientationManager(getActivity(), SensorManager.SENSOR_DELAY_NORMAL, this); orientationManager.enable(); } @Override public void onOrientationChange(ScreenOrientation screenOrientation) { switch(screenOrientation){ case PORTRAIT: case REVERSED_PORTRAIT: MainActivityBase.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); break; case REVERSED_LANDSCAPE: MainActivityBase.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); break; case LANDSCAPE: MainActivityBase.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); break; } } } 

如果你禁用屏幕方向改变,那么显然onConfigurationChanged永远不会被调用…

我认为唯一的办法是使用加速度传感器,检查这个链接 。

我需要一个解决scheme,只有按需提供给我的方向。 这个为我工作:

 public class SensorOrientationChecker { public final String TAG = getClass().getSimpleName(); int mOrientation = 0; private SensorEventListener mSensorEventListener; private SensorManager mSensorManager; private static SensorOrientationChecker mInstance; public static SensorOrientationChecker getInstance() { if (mInstance == null) mInstance = new SensorOrientationChecker(); return mInstance; } private SensorOrientationChecker() { mSensorEventListener = new Listener(); Context applicationContext = GlobalData.getInstance().getContext(); mSensorManager = (SensorManager) applicationContext.getSystemService(Context.SENSOR_SERVICE); } /** * Call on activity onResume() */ public void onResume() { mSensorManager.registerListener(mSensorEventListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); } /** * Call on activity onPause() */ public void onPause() { mSensorManager.unregisterListener(mSensorEventListener); } private class Listener implements SensorEventListener { @Override public void onSensorChanged(SensorEvent event) { float x = event.values[0]; float y = event.values[1]; if (x<5 && x>-5 && y > 5) mOrientation = 0; else if (x<-5 && y<5 && y>-5) mOrientation = 90; else if (x<5 && x>-5 && y<-5) mOrientation = 180; else if (x>5 && y<5 && y>-5) mOrientation = 270; //Log.e(TAG,"mOrientation="+mOrientation+" ["+event.values[0]+","+event.values[1]+","+event.values[2]+"]"); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } } public int getOrientation(){ return mOrientation; } } 

为了进入你想要在该活动中的清单文件中设置

 android:configChanges="orientation|keyboardHidden" 

然后当用户旋转手机它将进入公共无效onConfigurationChanged()方法。 也删除

 android:screenOrientation="portrait" 

来自同一个活动。

如果有人正在寻找一个Webview / JavaScript解决scheme的问题,下面可以做到这一点。

这将触发窗口上的自定义“翻转”事件,jquery具有“额外参数”。 它也设置window.flip,类似于window.orientation:

 $(window).on('flip',function(ev,angle,orientation) { console.log(angle,orientation); alert(window.flip); }); 

 if (window.DeviceOrientationEvent) { jQuery.flip = { debug : false, interval : 1000, checked : false, betaflat : 25, gammaflat : 45, orientation : 'portrait-primary', angles : { 'portrait-primary' : 0, 'portrait-secondary' : 0, 'landscape-primary' : 90, 'landscape-secondary' : -90 }, timer : null, check : function(ev) { if (!this.checked) { var trigger=false; if (this.debug) console.log([ev.alpha,ev.beta,ev.gamma]); if (ev.beta>this.betaflat) { // if beta is big its portrait if (this.debug) console.log('beta portrait pri'); if (this.orientation!='portrait-primary') { this.orientation='portrait-primary'; trigger=true; } } else if (ev.beta<-this.betaflat) { // if beta is big its portrait if (this.debug) console.log('beta portrait sec'); if (this.orientation!='portrait-secondary') { this.orientation='portrait-secondary'; trigger=true; } } else if (ev.gamma>this.gammaflat) { // else if gamma is big its landscape if (this.debug) console.log('gamma landscape pri'); if (this.orientation!='landscape-primary') { this.orientation='landscape-primary'; trigger=true; } } else if (ev.gamma<-this.gammaflat) { // else if gamma is big its landscape if (this.debug) console.log('gamma landscape sec'); if (this.orientation!='landscape-secondary') { this.orientation='landscape-secondary'; trigger=true; } } if (trigger) { if (this.debug) console.log('trigger flip'); window.flip = this.angles[this.orientation]; $(window).trigger('flip',[window.flip,this.orientation]); this.checked=true; } } } } $(document).ready(function() { setInterval(function() {jQuery.flip.checked=false},jQuery.flip.interval); $(window).on('deviceorientation',function(ev) { jQuery.flip.check(ev.originalEvent) }); }); } else { if (this.debug) console.log('DeviceOrientationEvent not supported'); } 

jQuery不是真的需要。 无论如何,我都需要它。