While playing the video, you may want to do a screen switch, but the user can set their own phone to turn off the screen rotation, this time need to think of other ways, such as: Gravity sensor.
public class Screenswitchutils {private static final String TAG = ScreenSwitchUtils.class.getSimpleName ();p rivate Volatile static screenswitchutils minstance;private Activity mactivity;//Whether it is a vertical private Boolean isportrait = True;private Sensormanager sm;private orientationsensorlistener listener;private Sensor sensor;private SensorManager sm1;private Sensor sensor1;private OrientationSensorListener1 listener1;private Handler mhandler = new Handler () {public void Handlem Essage (Message msg) {switch (msg.what) {case 888:int orientation = msg.arg1;if (Orientation > $ && Orientation < 135) {} else if (Orientation > 135 && Orientation < 225) {} else if (Orientation > 225 && o Rientation < 315) {if (isportrait) {log.e ("test", "Switch to horizontal screen"), mactivity.setrequestedorientation (0); isportrait = false;}} else if ((Orientation > 315 && Orientation < 360) | | (Orientation > 0 && Orientation < 45)) {if (!isportrait) {log.e ("test", "Switch to vertical screen"), MACtivity.setrequestedorientation (1); isportrait = True;}} break;default:break;}};};/ * * Returns screenswitchutils singleton **/public static Screenswitchutils init (context context) {if (minstance = = null) { Synchronized (Screenswitchutils.class) {if (minstance = = null) {minstance = n EW screenswitchutils (context); }}} return minstance; } Private Screenswitchutils (context context) {LOG.D (TAG, "init orientation listener."); Register gravity sensor, monitor screen rotation sm = (Sensormanager) context.getsystemservice (Context.sensor_service); Sensor = Sm.getdefaultsensor (Sensor.type_accelerometer); Listener = new Orientationsensorlistener (Mhandler); Activate SM According to the same direction after rotation/click Full screen. SM1 = (Sensormanager) context.getsystemservice (Context.sensor_service); Sensor1 = Sm1.getdefaultsensor (Sensor.type_accelerometer); Listener1 = new OrientationSensorListener1 (); } /**Start monitoring */public void start (activity activity) {LOG.D (TAG, "Start orientation listener."); Mactivity = activity; Sm.registerlistener (listener, sensor, SENSORMANAGER.SENSOR_DELAY_UI); }/** Stop monitoring */public void Stop () {LOG.D (TAG, "Stop orientation listener."); Sm.unregisterlistener (listener); Sm1.unregisterlistener (Listener1); }/** * Manual vertical and horizontal screen switching direction */public void Togglescreen () {Sm.unregisterlistener (listener); Sm1.registerlistener (Li Stener1, sensor1,sensormanager.sensor_delay_ui); if (isportrait) {isportrait = false;// Toggle Cheng mactivity.setrequestedorientation (activityinfo.screen_orientation_landscape);} else {isportrait = true;//switch to vertical screen mactivity.setrequestedorientation (activityinfo.screen_orientation_portrait);}} public Boolean isportrait () {return this.isportrait; }/** * Gravity Sensor listener */public class Orientationsensorlistener implements Sensoreventlistener {private static final int _ data_x = 0;private static final int _data_y= 1;private static final int _data_z = 2;public static final int orientation_unknown = -1;private Handler rotatehandler;pu Blic Orientationsensorlistener (Handler Handler) {rotatehandler = Handler;} public void onaccuracychanged (Sensor arg0, int arg1) {}public void onsensorchanged (Sensorevent event) {float[] values = Ev Ent.values;int Orientation = orientation_unknown;float X =-values[_data_x];float Y =-values[_data_y];float Z =-values[_ Data_z];float magnitude = x * x + Y * y;//Don ' t trust the angle if the magnitude are small compared to the y//Valueif (ma Gnitude * 4 >= Z * z) {//screen rotation float Oneeightyoverpi = 57.29577957855f;float angle = (float) math.atan2 (-y, X) * Oneeig Htyoverpi;orientation =-(int) math.round (angle);//normalize to 0-359 rangewhile (orientation >=) {Orientati On-= 360;} while (Orientation < 0) {orientation + = 360;}} if (Rotatehandler! = null) {rotatehandler.obtainmessage (888, orientation, 0). Sendtotarget ();}} public class OrientationSensorListener1 Implements Sensoreventlistener {private static final int _data_x = 0;private static final int _data_y = 1;private static Final int _data_z = 2;public static final int orientation_unknown = -1;public OrientationSensorListener1 () {}public void O Naccuracychanged (Sensor arg0, int arg1) {}public void onsensorchanged (Sensorevent event) {float[] values = event.values;i NT Orientation = orientation_unknown;float X =-values[_data_x];float Y =-values[_data_y];float Z =-values[_data_z];floa t magnitude = x * x + Y * y;//Don ' t trust the angle if the magnitude is small compared to the y//valueif (magnitude * 4 >= Z * z) {///screen rotation float Oneeightyoverpi = 57.29577957855f;float angle = (float) math.atan2 (-y, X) * Oneeightyoverpi;or Ientation =-(int) math.round (angle);//normalize to 0-359 rangewhile (orientation >=) {orientation-= 360;} while (Orientation < 0) {orientation + = 360;}} if (Orientation > 225 && Orientation < 315) {//detects that the current is actually a horizontal screen if (!isportrait) {sm.registErlistener (Listener, sensor,sensormanager.sensor_delay_ui); Sm1.unregisterlistener (Listener1);}} else if ((Orientation > 315 && Orientation < 360) | | (Orientation > 0 && Orientation < 45)) {//detects that the current is actually vertical if (isportrait) {Sm.registerlistener (Listener, SENSOR,SENSORMANAGER.SENSOR_DELAY_UI); Sm1.unregisterlistener (Listener1);}}}}
Reprint please indicate source: http://blog.csdn.net/goldenfish1919/article/details/47423131
When used:
public class Mainactivity extends Activity implements Onclicklistener {private Screenswitchutils instance;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); instance = Screenswitchutils.init (This.getapplicationcontext ());} @Overrideprotected void OnStart () {Super.onstart (); Instance.start (this);} @Overrideprotected void OnStop () {super.onstop (); Instance.stop ();} @SuppressLint ("Newapi") @Overridepublic void onconfigurationchanged (Configuration newconfig) { Super.onconfigurationchanged (Newconfig); LOG.E ("Test", "onconfigurationchanged"), if (instance.isportrait ()) {//Switch to vertical screen layoutparams params1 = new Relativelayout.layoutparams (ScreenWidth, densityutil.dip2px (this)); Videoview.setlayoutparams (PARAMS1); Toast.maketext (Getapplicationcontext (), "vertical screen", 0). Show (); LOG.E ("Test", "vertical Screen");} else {//toggle Cheng layoutparams params1 = new Relativelayout.layoutparams (screenheight, screenwidth); Videoview.setlayoutparams (PARAMS1); Toast.maketext (Getapplicationcontext (), "HorizontalScreen ", 0). Show (); LOG.E ("Test", "horizontal Screen");}} @Overridepublic void OnClick (View arg0) {switch (Arg0.getid ()) {case R.id.iv_stretch:instance.togglescreen ();
After calling Activity.setrequestedorientation (), the activity.onconfigurationchanged () is triggered and the playback interface can be resized here.
Reference: http://download.csdn.net/download/liubo080852/8446445
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android-using gravity sensor to switch the screen