문제

I am trying to make my device vibrate when I touch an object on Screen. I am using this code:

 Vibrator v = (Vibrator) getSystemService(getApplicationContext().VIBRATOR_SERVICE); 
 v.vibrate(300);    

with the permission in the manifest file but I don't seem to get any results. Any suggestions? Also, my hardware supports vibrate.

도움이 되었습니까?

해결책

please try this :

Button b = (Button) findViewById(R.id.button1);
    b.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            Vibrator vb = (Vibrator)   getSystemService(Context.VIBRATOR_SERVICE);
            vb.vibrate(100);
            return false;
        }
    });

and add this permission to manifest.xml

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

다른 팁

According to this answer, you can perform haptic feedback (vibrate) without asking for any extra permissions. Look at performHapticFeedback method.

View view = findViewById(...)
view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);

Note: I have not tested this code.

This will vibrate once, when user touches view (will not keep vibrating when user sliding his finger on the view!):

@Override
public boolean onTouch(View view, MotionEvent event) {

    if(event.getAction() == MotionEvent.ACTION_DOWN) {
        Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(VIBRATE_DURATION_MS);
    }
    return true;
}

And as Ramesh said, allow permission in manifest:

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

If anyone looking for kotlin,

val vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibrator.vibrator(durationInMilliSeconds)

and for Android-O and newer,

val vibrationEffect = VibrationEffect.createOneShot(vibrationDuration, vibrationAmplitude)
vibrator.vibrator(vibrationEffect) 

while to cancel vibration,

vibrate.cancel()

you also need to add permission in your AndroidManifest.xml

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

working for me kotlin ext fun

for the haptic effect, vibro has 5 milliseconds!! (SHORT_HAPTIC_FEEDBACK_DURATION)

fun Context.performHapticFeedback() {
    val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val vibrationEffect = VibrationEffect.createOneShot(HAPTIC_FEEDBACK_DURATION, VibrationEffect.DEFAULT_AMPLITUDE)
        vibrator.vibrate(vibrationEffect)
    } else {
        vibrator.vibrate(TimeUnit.MILLISECONDS.toMillis(SHORT_HAPTIC_FEEDBACK_DURATION))
    }
}

private const val SHORT_HAPTIC_FEEDBACK_DURATION = 5L

usage

addOnItemTouchListener(ItemTouchListener { position, event ->
                if (event.action == MotionEvent.ACTION_DOWN) {
                    context.performHapticFeedback()  
                }  
            })

permission

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

good luck ✌ :))

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top