본문 바로가기

StackOverflow

[Android] 안드로이드의 진동기능을 어떻게 이용하나요?

http://stackoverflow.com/questions/13950338/how-to-make-an-android-device-vibrate

참고: http://developer.android.com/reference/android/os/Vibrator.html


Q: 안드로이드의 진동기능을 어떻게 이용하나요?

안드로이드 앱을 만드는 중입니다. 이제, 특정 동작을 하면 진동이 발생하게 구현하고 싶은데, 어떻게 하면 되나요?

(질문자: Billie)




A: 이렇게 해보세요.

 import android.os.Vibrator;
 ...
 Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
 // Vibrate for 500 milliseconds
 v.vibrate(500);


주의:

permission을 AndroidManifest.xml에 꼭 포함시켜야 합니다.

<uses-permission android:name="android.permission.VIBRATE"/>

(답변자: Paresh Mayani)



A: 아래와 같이 하면 무한반복 할 수 있습니다.

// 딜레이를 주고 시작, 100 밀리세컨드 진동
// 1000 밀리세컨드 휴식
long[] pattern = {0, 100, 1000};

// 0은 무한반복
// 이 자리에 다른 숫자를 입력하면 해당 횟수만큼 반복
v.vibrate(pattern, 0);


아래와 같이 취소할 수 있습니다.

v.cancel();


아래와 같이 특정 패턴으로 진동시킬 수 있습니다.

Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// 딜레이를 주고 시작
// 진동, 휴식, 진동, 휴식...
long[] pattern = {0, 100, 1000, 300, 200, 100, 500, 200, 100};

// -1은 한 번만 진동하라는 의미
v.vibrate(pattern, -1);


해당 단말에 진동기능이 있는지 확인하려면 아래와 같이 하면 됩니다.

Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// Output yes if can vibrate, no otherwise
if (v.hasVibrator()) {
    Log.v("Can Vibrate", "YES");
} else {
    Log.v("Can Vibrate", "NO");
}

(답변자: Liam George Betsworth)


_

I wrote an Android application. Now, I want to make the device vibrate when a certain action occurs. How can I do this?

shareeditflag

_

449down voteaccepted

Try:

 import android.os.Vibrator;
 ...
 Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
 // Vibrate for 500 milliseconds
 v.vibrate(500);

Note:

Don't forget to include permission in AndroidManifest.xml file:

<uses-permission android:name="android.permission.VIBRATE"/>
shareeditflag


Grant Vibration Permission

Before you start implementing any vibration code, you have to give your application the permission to vibrate:

<uses-permission android:name="android.permission.VIBRATE"/>

Make sure to include this line in your AndroidManifest.xml file.

Import the Vibration Library

Most IDEs will do this for you, but here is the import statement if yours doesn't:

 import android.os.Vibrator;

Make sure this in the activity where you want the vibration to occur.

How to Vibrate for a Given Time

In most circumstances, you'll be wanting to vibrate the device for a short, predetermined amount of time. You can achieve this by using the vibrate(long milliseconds) method. Here is a quick example:

// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// Vibrate for 400 milliseconds
v.vibrate(400);

That's it, simple!

How to Vibrate Indefinitely

It may be the case that you want the device to continue vibrating indefinitely. For this, we use the vibrate(long[] pattern, int repeat) method:

// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// Start without a delay
// Vibrate for 100 milliseconds
// Sleep for 1000 milliseconds
long[] pattern = {0, 100, 1000};

// The '0' here means to repeat indefinitely
// '0' is actually the index at which the pattern keeps repeating from (the start)
// To repeat the pattern from any other point, you could increase the index, e.g. '1'
v.vibrate(pattern, 0);

When you're ready to stop the vibration, just call the cancel() method:

v.cancel();

How to use Vibration Patterns

If you want a more bespoke vibration, you can attempt to create your own vibration patterns:

// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// Start without a delay
// Each element then alternates between vibrate, sleep, vibrate, sleep...
long[] pattern = {0, 100, 1000, 300, 200, 100, 500, 200, 100};

// The '-1' here means to vibrate once, as '-1' is out of bounds in the pattern array
v.vibrate(pattern, -1);

More Complex Vibrations

There are multiple SDKs that offer a more comprehensive range of haptic feedback. One that I use for special effects is Immersion's Haptic Development Platform for Android.

Troubleshooting

If your device won't vibrate, first make sure that it can vibrate:

// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// Output yes if can vibrate, no otherwise
if (v.hasVibrator()) {
    Log.v("Can Vibrate", "YES");
} else {
    Log.v("Can Vibrate", "NO");
}

Secondly, please ensure that you've given your application the permission to vibrate! Refer back to the first point.

shareeditflag