diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bfae48b..d41fd3f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -19,7 +19,9 @@ android:fitsSystemWindows="true" android:name=".application.InitApplication" android:theme="@style/Theme.PdaWeb" - android:usesCleartextTraffic="true"> + android:usesCleartextTraffic="true" + android:configChanges="orientation|screenSize|keyboardHidden" + > diff --git a/app/src/main/java/chaoran/business/activity/MainActivity.java b/app/src/main/java/chaoran/business/activity/MainActivity.java index a9e844d..fa8362a 100644 --- a/app/src/main/java/chaoran/business/activity/MainActivity.java +++ b/app/src/main/java/chaoran/business/activity/MainActivity.java @@ -5,6 +5,7 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ActivityInfo; +import android.content.res.Configuration; import android.graphics.Bitmap; import android.hardware.SensorManager; import android.media.MediaPlayer; @@ -52,29 +53,14 @@ public class MainActivity extends AppCompatActivity implements ResultListener{ private ProgressBar progressBar; private ActionBar actionBar; -// private OrientationEventListener orientationEventListener; - -// public static int rotate = 0; // 屏幕旋转的角度 @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); -// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); // 始终竖屏,由重力感应 -// 实时监控屏幕旋转 -// orientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) -// { -// -// @Override -// public void onOrientationChanged(int i) { -// rotate = i; -// } -// }; setContentView(R.layout.activity_main); initView(); initData(); -// this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); - this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); -// this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR); } @@ -205,7 +191,7 @@ public class MainActivity extends AppCompatActivity implements ResultListener{ } @Override protected void onDestroy() { - adapter.stop(); +// adapter.stop(); super.onDestroy(); if (diPlayer != null) { diPlayer.stop(); @@ -306,4 +292,37 @@ public class MainActivity extends AppCompatActivity implements ResultListener{ } }; + + /* 监听角度的变化 + class MyOrientationDetector extends OrientationEventListener { + + private int rotate; + + public MyOrientationDetector(Context context) { + super(context); + } + + @Override + public void onOrientationChanged(int orientation) { + //假设屏幕旋转被打开。则设置屏幕可旋转 + //0-57度 125-236度 306-360度 这些区间范围内为竖屏 + //58-124度 237-305度 这些区间范围内为横屏 + if (orientation >=0 && orientation <= 57 ) { + rotate = 1; // 正竖屏 + }else if (orientation >=125 && orientation <= 236 ) { + rotate = 2; // 倒竖屏 + }else if (orientation >=306 && orientation <= 360 ) { + rotate = 1; // 正竖屏 + }else if (orientation >=58 && orientation <= 124 ) { + rotate = 3; // 正横屏 + }else if (orientation >=237 && orientation <= 305 ) { + rotate = 4; // 倒横屏 + }else { + rotate = 1; // 默认正竖屏 + } + System.out.println("屏幕旋转角度监控::" + rotate); + } + + }*/ + } \ No newline at end of file diff --git a/app/src/main/java/chaoran/business/utils/LocalAddressUtil.java b/app/src/main/java/chaoran/business/utils/LocalAddressUtil.java index 8e9dfa6..475d684 100644 --- a/app/src/main/java/chaoran/business/utils/LocalAddressUtil.java +++ b/app/src/main/java/chaoran/business/utils/LocalAddressUtil.java @@ -7,7 +7,9 @@ import android.content.Context; import android.content.pm.ActivityInfo; import android.os.Build; import android.util.Log; +import android.view.Surface; import android.view.View; +import android.view.WindowManager; import android.webkit.JavascriptInterface; import java.net.Inet4Address; @@ -135,18 +137,25 @@ public class LocalAddressUtil { @SuppressLint("JavascriptInterface") @JavascriptInterface - public void rotateScreen(int type) { - // 使用的是view对象,旋转,出现的问题,键盘并没有出现旋转 -// view.setRotationX(type); -// view.setRotationY(type); + public void rotateScreen() { + int angle = ((WindowManager)activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation(); -// activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); - if (type == 0) { - activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); - }else { - activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); + switch (angle) { + case Surface.ROTATION_0: + activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); + break; + case Surface.ROTATION_90: + activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); + break; + case Surface.ROTATION_180: + activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); + break; + case Surface.ROTATION_270: + activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + break; + default: + break; } -// System.out.println("MainActivity.rotate :: " + MainActivity.rotate); }