안드로이드 진동 감지, 가속도계, 진동 발생

2020. 3. 8. 13:05실천해본것들

작동하는 api : 25 . nougat(7.1)

 

manifest

1
2
<!--    가속도계 센서 - 진동, 운동가속을 감지하는 기능 사용-->
    <uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

shake detecter class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
 
// detector를 따로 선언
public class ShakeDetector
    // 센서 이벤트를 들을 listener를 충족하는 class
        implements SensorEventListener {
    /*
     * The gForce that is necessary to register as shake.
     * Must be greater than 1G (one earth gravity unit).
     * You can install "G-Force", by Blake La Pierre
     * from the Google Play Store and run it to see how
     *  many G's it takes to register a shake
     */
    // 중력, 중력가속도을 기준으로 삼아서 진동, 움직임을 측정한다.
    // 흔들림 감지할 때 기준이 되는 가해지는 힘
    private static final float SHAKE_THRESHOLD_GRAVITY = 2.7F;
    // 흔들림 감지할때 최소 0.5초를 기준으로 측정한다.
    private static final int SHAKE_SLOP_TIME_MS = 500;
    // 흔드는 횟수는 3초마다 초기화
    private static final int SHAKE_COUNT_RESET_TIME_MS = 3000;
    // listener
    private OnShakeListener mListener;
    // 시간 기록용
    private long mShakeTimestamp;
    // 횟수
    private int mShakeCount;
    // listener setting
    public void setOnShakeListener(OnShakeListener listener) {
        this.mListener = listener;
    }
    // listener interface
    public interface OnShakeListener {
        public void onShake(int count);
    }
    // 정확도가 변할 때? 사용하지 않는다
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // ignore
    }
    // 센서 method.
    @Override
    public void onSensorChanged(SensorEvent event) {
        if (mListener != null) {
            // x,y,z 축의 값을 받아온다
            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;
            // gforce는 중력가속도를 포함하는 물체가 받는 힘
            // 1일때는 평소에 받는 중력(정지)
            // 1이하일때(아래로 떨어지며 힘을 덜받을 때)
            // 1이상일 때(위로 올라가면서 힘을 더 받을 때)
            // 단순히 힘의 크기를 계산하기 때문에 피타고라스로 구한다
            // gForce will be close to 1 when there is no movement.
            float gForce = (float)Math.sqrt(gX * gX + gY * gY + gZ * gZ);
            // 진동을 감지했을 때
            // gforce가 기준치 이상일 경우
            if (gForce > SHAKE_THRESHOLD_GRAVITY) {
                final long now = System.currentTimeMillis();
                // 진동 간격이 너무 짧을 때는 무시
                // ignore shake events too close to each other (500ms)
                if (mShakeTimestamp + SHAKE_SLOP_TIME_MS > now) {
                    return;
                }
                // 3초 이상 걸렸을 때 reset한다
                // reset the shake count after 3 seconds of no shakes
                if (mShakeTimestamp + SHAKE_COUNT_RESET_TIME_MS < now) {
                    mShakeCount = 0;
                }
                // 업데이트한다
                mShakeTimestamp = now;
                mShakeCount++;
                // 흔들렸을 때 행동을 설정한다
                mListener.onShake(mShakeCount);
            }
        }
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

적용시

 

변수 선언

1
2
3
4
    // The following are used for the shake detection
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private ShakeDetector mShakeDetector;
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

생명주기 고려한 적용. handleShakeEvent에서 진동을 감지할 때 할 작업을 정의한다

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reward);
        // ShakeDetector initialization
        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) {
                /*
                 * The following method, "handleShakeEvent(count):" is a stub //
                 * method you would use to setup whatever you want done once the
                 * device has been shook.
                 */
                handleShakeEvent(count);
            }
        });
    }    
    @Override
    public void onResume() {
        super.onResume();
        // Add the following line to register the Session Manager Listener onResume
        mSensorManager.registerListener(mShakeDetector, mAccelerometer,    SensorManager.SENSOR_DELAY_UI);
    }
    // background 상황에서도 흔들림을 감지하고 적용할 필요는 없다
    @Override
    public void onPause() {
        // Add the following line to unregister the Sensor Manager onPause
        mSensorManager.unregisterListener(mShakeDetector);
        super.onPause();
    }
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

 

원본 링크

http://jasonmcreynolds.com/?p=388

 

 

+ 추가 진동발생 기능(바이브레이터)

매니페스트

1
2
<!--    진동을 울리는 권한 요청-->
    <uses-permission android:name = "android.permission.VIBRATE"/>
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

적용

1
2
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

 

원본 링크

https://mine-it-record.tistory.com/238