| @@ -1,17 +1,17 @@ | |||||
| package com.nsgk.ruralWeb; | package com.nsgk.ruralWeb; | ||||
| import android.annotation.SuppressLint; | import android.annotation.SuppressLint; | ||||
| import androidx.appcompat.app.AppCompatActivity; | |||||
| import android.os.Bundle; | import android.os.Bundle; | ||||
| import android.view.KeyEvent; | import android.view.KeyEvent; | ||||
| import android.webkit.WebResourceRequest; | import android.webkit.WebResourceRequest; | ||||
| import android.webkit.WebSettings; | import android.webkit.WebSettings; | ||||
| import android.webkit.WebView; | import android.webkit.WebView; | ||||
| import android.webkit.WebViewClient; | import android.webkit.WebViewClient; | ||||
| import android.widget.EditText; | |||||
| import androidx.appcompat.app.AlertDialog; | |||||
| import androidx.appcompat.app.AppCompatActivity; | |||||
| import com.nsgk.ruralWeb.databinding.ActivityFullscreenBinding; | |||||
| /** | /** | ||||
| * An example full-screen activity that shows and hides the system UI (i.e. | * An example full-screen activity that shows and hides the system UI (i.e. | ||||
| @@ -21,6 +21,8 @@ public class FullscreenActivity extends AppCompatActivity { | |||||
| private WebView mWebView; | private WebView mWebView; | ||||
| private SensorManagerHelper sensorManagerHelper; | |||||
| @SuppressLint("SetJavaScriptEnabled") | @SuppressLint("SetJavaScriptEnabled") | ||||
| @Override | @Override | ||||
| protected void onCreate(Bundle savedInstanceState) { | protected void onCreate(Bundle savedInstanceState) { | ||||
| @@ -37,8 +39,18 @@ public class FullscreenActivity extends AppCompatActivity { | |||||
| return false; | return false; | ||||
| } | } | ||||
| }); | }); | ||||
| EditText textView = new EditText(this); | |||||
| AlertDialog alertDialog = new AlertDialog.Builder(this) | |||||
| .setView(textView) | |||||
| .setTitle("修改地址") | |||||
| .setPositiveButton("确定", (dialogInterface, i) -> mWebView.loadUrl("http://"+textView.getText().toString())) | |||||
| .create(); | |||||
| mWebView.loadUrl("http://www.baidu.com"); | mWebView.loadUrl("http://www.baidu.com"); | ||||
| sensorManagerHelper = new SensorManagerHelper(this); | |||||
| sensorManagerHelper.setOnShakeListener(alertDialog::show); | |||||
| } | } | ||||
| @Override | @Override | ||||
| @@ -0,0 +1,114 @@ | |||||
| package com.nsgk.ruralWeb; | |||||
| import android.content.Context; | |||||
| import android.hardware.Sensor; | |||||
| import android.hardware.SensorEvent; | |||||
| import android.hardware.SensorEventListener; | |||||
| import android.hardware.SensorManager; | |||||
| public class SensorManagerHelper implements SensorEventListener { | |||||
| // 速度阈值,当摇晃速度达到这值后产生作用 | |||||
| private final int SPEED_SHRESHOLD = 5000; | |||||
| // 两次检测的时间间隔 | |||||
| private final int UPTATE_INTERVAL_TIME = 50; | |||||
| // 传感器管理器 | |||||
| private SensorManager sensorManager; | |||||
| // 传感器 | |||||
| private Sensor sensor; | |||||
| // 重力感应监听器 | |||||
| private OnShakeListener onShakeListener; | |||||
| // 上下文对象context | |||||
| private Context context; | |||||
| // 手机上一个位置时重力感应坐标 | |||||
| private float lastX; | |||||
| private float lastY; | |||||
| private float lastZ; | |||||
| // 上次检测时间 | |||||
| private long lastUpdateTime; | |||||
| public SensorManagerHelper(Context context) { | |||||
| this.context = context; | |||||
| start(); | |||||
| } | |||||
| /** | |||||
| * 开始检测 | |||||
| */ | |||||
| public void start() { | |||||
| // 获得传感器管理器 | |||||
| sensorManager = (SensorManager) context | |||||
| .getSystemService(Context.SENSOR_SERVICE); | |||||
| if (sensorManager != null) { | |||||
| // 获得重力传感器 | |||||
| sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); | |||||
| } | |||||
| // 注册 | |||||
| if (sensor != null) { | |||||
| sensorManager.registerListener(this, sensor, | |||||
| SensorManager.SENSOR_DELAY_GAME); | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 停止检测 | |||||
| */ | |||||
| public void stop() { | |||||
| sensorManager.unregisterListener(this); | |||||
| } | |||||
| /** | |||||
| * 摇晃监听接口 | |||||
| */ | |||||
| public interface OnShakeListener { | |||||
| void onShake(); | |||||
| } | |||||
| /** | |||||
| * 设置重力感应监听器 | |||||
| */ | |||||
| public void setOnShakeListener(OnShakeListener listener) { | |||||
| onShakeListener = listener; | |||||
| } | |||||
| @Override | |||||
| public void onAccuracyChanged(Sensor sensor, int accuracy) { | |||||
| } | |||||
| /** | |||||
| * 重力感应器感应获得变化数据 | |||||
| * android.hardware.SensorEventListener#onSensorChanged(android.hardware | |||||
| * .SensorEvent) | |||||
| */ | |||||
| @Override | |||||
| public void onSensorChanged(SensorEvent event) { | |||||
| // 现在检测时间 | |||||
| long currentUpdateTime = System.currentTimeMillis(); | |||||
| // 两次检测的时间间隔 | |||||
| long timeInterval = currentUpdateTime - lastUpdateTime; | |||||
| // 判断是否达到了检测时间间隔 | |||||
| if (timeInterval < UPTATE_INTERVAL_TIME) return; | |||||
| // 现在的时间变成last时间 | |||||
| lastUpdateTime = currentUpdateTime; | |||||
| // 获得x,y,z坐标 | |||||
| float x = event.values[0]; | |||||
| float y = event.values[1]; | |||||
| float z = event.values[2]; | |||||
| // 获得x,y,z的变化值 | |||||
| float deltaX = x - lastX; | |||||
| float deltaY = y - lastY; | |||||
| float deltaZ = z - lastZ; | |||||
| // 将现在的坐标变成last坐标 | |||||
| lastX = x; | |||||
| lastY = y; | |||||
| lastZ = z; | |||||
| double speed = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ | |||||
| * deltaZ) | |||||
| / timeInterval * 10000; | |||||
| // 达到速度阀值,发出提示 | |||||
| if (speed >= SPEED_SHRESHOLD) { | |||||
| onShakeListener.onShake(); | |||||
| } | |||||
| } | |||||
| } | |||||