Development/Android

[Android] vibrate() func deprecated 대응

SeungYong.Lee 2025. 3. 14. 14:11
반응형
((Vibrator)AppCore.context.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(10);

- 기존 코드에서 사용하던 디바이스 진동 효과를 발생시키는 vibrate 코드가 deprecated 되어 있었다.

 

- 10m/s 동안만 일시적으로 진동을 울리는 코드인데, Android O(26)부터 아래와 같이 수정이 필요하다.

Vibrator vibrator = (Vibrator) AppCore.context.getSystemService(Context.VIBRATOR_SERVICE);
if (vibrator != null && vibrator.hasVibrator()) { // 진동 기능이 있는지 확인
        vibrator.vibrate(VibrationEffect.createOneShot(10, VibrationEffect.DEFAULT_AMPLITUDE));
}

 

- Waveform 함수로 반복 진동 또한 구성할 수 있다.

public static VibrationEffect createOneShot(long milliseconds, int amplitude) {
    throw new RuntimeException("Stub!");
}

public static VibrationEffect createWaveform(long[] timings, int repeat) {
    throw new RuntimeException("Stub!");
}

public static VibrationEffect createWaveform(long[] timings, int[] amplitudes, int repeat) {
    throw new RuntimeException("Stub!");
}
반응형