To detect phone shaking in an Android app using Java, you can use the accelerometer sensor. Here’s a simple example to get you started:
- Add Permissions: First, add the necessary permissions to your AndroidManifest.xml file
- <uses-permission android:name="android.permission.BODY_SENSORS" />
Create a ShakeDetector Class: This class will handle the shake detection logic.
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class ShakeDetector implements SensorEventListener {
private static final float SHAKE_THRESHOLD_GRAVITY = 2.7F; private static final int SHAKE_SLOP_TIME_MS = 500; private static final int SHAKE_COUNT_RESET_TIME_MS = 3000; private OnShakeListener mListener; private long mShakeTimestamp; private int mShakeCount; public interface OnShakeListener { void onShake(int count); } public void setOnShakeListener(OnShakeListener listener) { this.mListener = listener; } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // Ignore } @Override public void onSensorChanged(SensorEvent event) { if (mListener != null) { float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; float gX = x / SensorManager.GRAVITY_EARTH; float gY = y / SensorManager.GRAVITY_EARTH; float gZ = z / SensorManager.GRAVITY_EARTH; float gForce = (float) Math.sqrt(gX * gX + gY * gY + gZ * gZ); if (gForce > SHAKE_THRESHOLD_GRAVITY) { final long now = System.currentTimeMillis(); if (mShakeTimestamp + SHAKE_SLOP_TIME_MS > now) { return; } if (mShakeTimestamp + SHAKE_COUNT_RESET_TIME_MS < now) { mShakeCount = 0; } mShakeTimestamp = now; mShakeCount++; mListener.onShake(mShakeCount); } } } }
Use the ShakeDetector in Your Activity:
import android.hardware.Sensor; import android.hardware.SensorManager; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private SensorManager mSensorManager; private Sensor mAccelerometer; private ShakeDetector mShakeDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); mShakeDetector = new ShakeDetector(); mShakeDetector.setOnShakeListener(new ShakeDetector.OnShakeListener() { @Override public void onShake(int count) { // Handle shake event here // For example, show a Toast message Toast.makeText(MainActivity.this, "Shake detected!", Toast.LENGTH_SHORT).show(); } }); } @Override protected void onResume() { super.onResume(); mSensorManager.registerListener(mShakeDetector, mAccelerometer, SensorManager.SENSOR_DELAY_UI); } @Override protected void onPause() { super.onPause(); mSensorManager.unregisterListener(mShakeDetector); } }
This code sets up a shake detector that triggers a toast message whenever a shake is detected. You can customize the onShake method to perform any action you need when a shake event occurs12.
Would you like more details on any part of this code?
'android source ' 카테고리의 다른 글
Sound, media 대신 ring mode로 설정 (0) | 2024.10.19 |
---|---|
sub class 에서 localbroadcaster로 main call 하기 (0) | 2024.09.21 |
String Utility (0) | 2024.09.16 |
Notification 보내기 (0) | 2024.09.16 |
resource 에서 text file 읽기 (0) | 2024.09.16 |