PDA旋转,不重置activity;采取配置化,是否强制竖屏、强制横屏

This commit is contained in:
2024-05-29 14:06:56 +08:00
parent 23df4da1ff
commit 4af4aac1e5
8 changed files with 164 additions and 13 deletions

View File

@ -11,7 +11,7 @@ android {
minSdk 28
targetSdk 28
versionCode 1
versionName "1.11"
versionName "1.12"
// 1.0 IDATA广播模式处理
// 1.1 霍尼韦尔的监听修改扫描网站二维码跳出程序监听失效调整、斑马PDA广播模式设置
@ -25,6 +25,7 @@ android {
// 1.9 海康威视 mv-idp5102 适配广播模式
// 1.10 bug 代码判断没有结束
// 1.11 霍尼韦尔EDA52
// 1.12 屏幕旋转采取配置化模式只能竖屏、横屏取消旋转屏幕就重置activity生命周期
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

View File

@ -24,7 +24,6 @@
android:name=".application.InitApplication"
android:theme="@style/Theme.PdaWeb"
android:usesCleartextTraffic="true"
android:configChanges="orientation|screenSize|keyboardHidden"
android:screenOrientation="user"
>
<uses-library android:name="com.symbol.emdk" android:required="false"/>
@ -36,6 +35,7 @@
android:label="@string/title_activity_setting_voice" />
<activity
android:name=".activity.MainActivity"
android:configChanges="orientation|screenSize"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -104,7 +104,8 @@
var data=JSON.stringify({
"address":document.getElementById('address').value,
"port":document.getElementById('port').value,
"path":document.getElementById('path').value
"path":document.getElementById('path').value,
"screen_rotation":document.getElementById('screen_rotation').value
});
window.NetworkSettingEngine.saveSetting(data);
window.View.reload();
@ -130,6 +131,10 @@
<label>访问子路径</label>
<input id="path" name="path" placeholder="访问子路径">
</div>
<div class="net-list">
<label>屏幕旋转</label>
<input id="screen_rotation" value="3" name="screen_rotation" placeholder="1:强制竖屏2:强制横屏3:横竖都转">
</div>
</div>
<div class="operation">
<button style="background: #1989fa;" onclick="saveSetting()">保存</button>

View File

@ -1,12 +1,16 @@
package chaoran.business.activity;
import android.annotation.SuppressLint;
import android.app.Activity;
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.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Build;
@ -16,7 +20,9 @@ import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.OrientationEventListener;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;
import android.webkit.*;
import android.widget.ProgressBar;
import android.widget.Toast;
@ -25,6 +31,8 @@ import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.Map;
@ -49,6 +57,12 @@ import chaoran.business.utils.StatusBarUtil;
*/
public class MainActivity extends AppCompatActivity implements ResultListener{
public static int SCREEN_ROTATION = 3; // 屏幕旋转的设置
MyOrientationDetector myOrientationDetector;
private static int latestOrientation = 0; // 添加一个标志位;最后屏幕的方向
private WebView webView;
private Adapter adapter;
private VoiceEngine voiceEngine;
@ -66,6 +80,8 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
//SCREEN_ORIENTATION_FULL_SENSOR 都可以旋转,但是在不打开旋转的情况下,一样可以旋转
//SCREEN_ORIENTATION_FULL_USER 关闭旋转则不会旋转了
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_USER);
myOrientationDetector = new MyOrientationDetector(this, this);
myOrientationDetector.enable();
}
@ -221,6 +237,7 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
// voiceEngine.reload();
super.onResume();
if (adapter != null) {
myOrientationDetector.disable();
adapter.stop2();
adapter.start();
}
@ -287,6 +304,7 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
String address = spf.getString("address", "").replaceAll(" ", "");
String path = spf.getString("path", "").replaceAll(" ", "");
String link = address.concat(":").concat(String.valueOf(port)).concat(path);
SCREEN_ROTATION = spf.getInt("screen_rotation", 3);
return link.startsWith("http://") ? link : "http://".concat(link);
}
@ -357,13 +375,16 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
});
}
/* 监听角度的变化
/* 监听角度的变化 */
class MyOrientationDetector extends OrientationEventListener {
private int rotate;
public MyOrientationDetector(Context context) {
private Activity activity;
public MyOrientationDetector(Context context, Activity activity) {
super(context);
this.activity = activity;
}
@Override
@ -372,21 +393,104 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
//0-57度 125-236度 306-360度 这些区间范围内为竖屏
//58-124度 237-305度 这些区间范围内为横屏
if (orientation >=0 && orientation <= 57 ) {
rotate = 1; // 正竖屏
rotate = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; // 正竖屏
}else if (orientation >=125 && orientation <= 236 ) {
rotate = 2; // 倒竖屏
rotate = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; // 倒竖屏
}else if (orientation >=306 && orientation <= 360 ) {
rotate = 1; // 正竖屏
rotate = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; // 正竖屏
}else if (orientation >=58 && orientation <= 124 ) {
rotate = 3; // 正横屏
rotate = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; // 正横屏
}else if (orientation >=237 && orientation <= 305 ) {
rotate = 4; // 倒横屏
rotate = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; // 倒横屏
}else {
rotate = 1; // 默认正竖屏
rotate = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; // 默认正竖屏
}
if (SCREEN_ROTATION == 1 || SCREEN_ROTATION == 3) {
if (rotate == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT || rotate == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
activity.setRequestedOrientation(rotate);
latestOrientation = rotate;
}
}
if (SCREEN_ROTATION == 2 || SCREEN_ROTATION == 3) {
if (rotate == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE || rotate == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
activity.setRequestedOrientation(rotate);
latestOrientation = rotate;
}
}
System.out.println("屏幕旋转角度监控::" + rotate);
}
}
/*class MyOrientationDetector implements SensorEventListener {
private Activity activity;
public MyOrientationDetector(Activity activity) {
this.activity = activity;
Sensor orientationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
sensorManager.registerListener(this, orientationSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent event) {
int angle = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
if (latestOrientation == angle) {
return;
}
if (SCREEN_ROTATION == 1) {
if (Surface.ROTATION_0 == angle) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
latestOrientation = angle;
}else if (Surface.ROTATION_180 == angle) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
latestOrientation = angle;
}
return;
}else if (SCREEN_ROTATION == 2) {
if (Surface.ROTATION_90 == angle) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
latestOrientation = angle;
}else if (Surface.ROTATION_270 == angle) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
latestOrientation = angle;
}
return;
}
int tmpAngle = latestOrientation;
latestOrientation = angle;
switch (angle) {
case Surface.ROTATION_0:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_90:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_180:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_270:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
default:
latestOrientation = tmpAngle;
break;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}*/
@SuppressLint("WrongConstant")
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
}

View File

@ -19,7 +19,7 @@ import chaoran.business.R;
public class NetworkSettingActivity extends AppCompatActivity {
private EditText address, path, port;
private EditText address, path, port, screen_rotation;
private Button save, cancel;
@Override
@ -35,12 +35,14 @@ public class NetworkSettingActivity extends AppCompatActivity {
address = findViewById(R.id.address);
path = findViewById(R.id.path);
port = findViewById(R.id.port);
screen_rotation = findViewById(R.id.screen_rotation);
save = findViewById(R.id.save);
cancel = findViewById(R.id.cancel);
SharedPreferences sharedPreferences = this.getSharedPreferences("crtech", Context.MODE_PRIVATE);
address.setText(sharedPreferences.getString("address", ""));
path.setText(sharedPreferences.getString("path", ""));
port.setText(String.valueOf(sharedPreferences.getInt("port", -1)));
screen_rotation.setText(String.valueOf(sharedPreferences.getInt("screen_rotation", 3)));
cancel.setOnClickListener((e) -> this.finish());
save.setOnClickListener((e) -> saveSetting());
}
@ -50,6 +52,7 @@ public class NetworkSettingActivity extends AppCompatActivity {
editor.putString("address", address.getText().toString().trim());
editor.putString("path", path.getText().toString().trim());
editor.putInt("port", Integer.parseInt(port.getText().toString().trim()));
editor.putInt("screen_rotation", Integer.parseInt(screen_rotation.getText().toString().trim()));
editor.commit();
this.finish();
}

View File

@ -12,6 +12,8 @@ public class NetworkSetting extends Setting {
private Integer port;
private String path;
private Integer screen_rotation;
public String getAddress() {
return address;
}
@ -35,4 +37,12 @@ public class NetworkSetting extends Setting {
public void setPath(String path) {
this.path = path;
}
public Integer getScreen_rotation() {
return screen_rotation;
}
public void setScreen_rotation(Integer screen_rotation) {
this.screen_rotation = screen_rotation;
}
}

View File

@ -11,6 +11,8 @@ package chaoran.business.engine.impl;
import android.content.Context;
import android.content.SharedPreferences;
import android.webkit.JavascriptInterface;
import chaoran.business.activity.MainActivity;
import chaoran.business.engine.SettingEngine;
import chaoran.business.engine.entity.NetworkSetting;
import chaoran.business.engine.entity.Setting;
@ -35,12 +37,14 @@ public class NetworkSettingEngine implements SettingEngine {
public Setting getSetting() {
SharedPreferences spf = context.getSharedPreferences("crtech", Context.MODE_PRIVATE);
Integer port = spf.getInt("port", -1);
Integer screen_rotation = spf.getInt("screen_rotation", 3);
String address = spf.getString("address", "").replaceAll(" ", "");
String path = spf.getString("path", "").replaceAll(" ", "");
NetworkSetting networkSetting = new NetworkSetting();
networkSetting.setAddress(address);
networkSetting.setPort(port);
networkSetting.setPath(path);
networkSetting.setScreen_rotation(screen_rotation);
return networkSetting;
}
@ -51,7 +55,9 @@ public class NetworkSettingEngine implements SettingEngine {
editor.putString("address", networkSetting.getAddress());
editor.putString("path", networkSetting.getPath());
editor.putInt("port", networkSetting.getPort());
editor.putInt("screen_rotation", networkSetting.getScreen_rotation());
editor.commit();
MainActivity.SCREEN_ROTATION = networkSetting.getScreen_rotation();
return true;
}

View File

@ -7,6 +7,28 @@
android:id="@+id/table">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="屏幕旋转:" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="150dp"
android:id="@+id/screen_rotation"
android:singleLine="true"
android:hint="1:强制竖屏2:强制横屏3:横竖都转" />
<TextView />
</TableRow>
<TableRow>
<TextView />