Created
May 30, 2016 03:24
-
-
Save BCsl/5cd2393d70e38598ab37f276bf43ea1a to your computer and use it in GitHub Desktop.
监听程序翻转
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class VerticalFlipReceiver implements SensorEventListener { | |
private static final String TAG = "VerticalFlipReceiver"; | |
private static final float CRITICAL_DOWN_ANGLE = -6.75f; | |
private static final float CRITICAL_UP_ANGLE = 6.75f; | |
private static final int Z_ORATIATION = 2; | |
private static final int SCREEN_FLIP_UP = 0; | |
private static final int SCREEN_FLIP_DOWN = 1; | |
private int mReverseDownFlg = -1; | |
private Context mContext; | |
private SensorManager mSensorManager; | |
private Sensor mSensor; | |
private OnFlipListener mOnFlipListener; | |
private boolean mIsStart; | |
public VerticalFlipReceiver(Context context) { | |
mContext = context; | |
} | |
/** | |
* 开始监听手机屏幕翻转 | |
*/ | |
public void start() { | |
LogUtils.d(TAG, "start listen to vertical flip:"); | |
if (mIsStart) { | |
return; | |
} | |
if (mSensorManager == null) { | |
mSensorManager = (SensorManager) mContext.getSystemService(Service.SENSOR_SERVICE); | |
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); | |
} | |
mIsStart = true; | |
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL); | |
} | |
/** | |
* 取消监听 | |
*/ | |
public void stop() { | |
LogUtils.d(TAG, "stop listen to vertical flip:"); | |
if (!mIsStart) { | |
return; | |
} | |
if (mSensorManager == null) { | |
LogUtils.w(TAG, "stop: Failed with null SensorManager!!!"); | |
return; | |
} | |
mIsStart = false; | |
mSensorManager.unregisterListener(this, mSensor); | |
mSensorManager = null; | |
mSensor = null; | |
} | |
public boolean isStart() { | |
return mIsStart; | |
} | |
/** | |
* 设置监听器监听屏幕翻转 | |
* | |
* @param onFlipListener | |
*/ | |
public void setOnFlipListener(OnFlipListener onFlipListener) { | |
mOnFlipListener = onFlipListener; | |
} | |
@Override | |
public void onSensorChanged(SensorEvent event) { | |
// if (BuildConfig.DEBUG) { | |
// Log.v(TAG, "onSensorChanged:x=>" + event.values[0] + ",y=>" + event.values[1] + ",z=>" + event.values[Z_ORATIATION]); | |
// } | |
if (event == null) { | |
return; | |
} | |
if (event.values[Z_ORATIATION] >= CRITICAL_UP_ANGLE && mReverseDownFlg != SCREEN_FLIP_UP) { | |
// if (BuildConfig.DEBUG) { | |
// Log.v(TAG, "onSensorChanged: screen up first"); | |
// } | |
mReverseDownFlg = SCREEN_FLIP_UP; | |
callOnFlip(); | |
} else if (event.values[Z_ORATIATION] <= CRITICAL_DOWN_ANGLE && mReverseDownFlg != SCREEN_FLIP_DOWN) { | |
// if (BuildConfig.DEBUG) { | |
// Log.v(TAG, "onSensorChanged:down next screen "); | |
// } | |
mReverseDownFlg = SCREEN_FLIP_DOWN; | |
callOnFlip(); | |
} | |
} | |
private void callOnFlip() { | |
if (mOnFlipListener == null) { | |
return; | |
} | |
switch (mReverseDownFlg) { | |
case SCREEN_FLIP_UP: | |
mOnFlipListener.onVerticalFlipUp(); | |
break; | |
case SCREEN_FLIP_DOWN: | |
mOnFlipListener.onVerticalFlipDown(); | |
break; | |
default: | |
throw new IllegalStateException("Unknown flag:" + mReverseDownFlg); | |
} | |
} | |
@Override | |
public void onAccuracyChanged(Sensor sensor, int accuracy) { | |
} | |
/** | |
* 监听手机的翻转事件 | |
* | |
* @author chensuilun | |
*/ | |
public interface OnFlipListener { | |
/** | |
* Called when the screen flip up | |
*/ | |
void onVerticalFlipUp(); | |
/** | |
* Called when the screen flip down | |
*/ | |
void onVerticalFlipDown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment