Skip to content Skip to sidebar Skip to footer

Detect The Device Is Vibrating?

I have used the below code to vibrate the device. public void vibrator() { try { Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); vibr

Solution 1:

No, you can't.

Same question is here.

You can check only if the device vibrating is supported:

VibratormVibrator= (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
booleanhasVibrator= mVibrator.hasVibrator();

See more:

Solution 2:

Introduction

The Vibrator class does not have the isVibrating() method that you are looking for. It uses services, so you cannot easily override Vibrator and add in the extra functionality.

ManagedVibrator

Below, is a ManagedVibrator class that is a wrapper for the Vibrator class. All Vibrator methods are included, with the additional isVibrating() method.

The constant vibration methods with signatures that accept long[] pattern are easy to track because cancel() needs to be called to end the vibration. However, the one time vibration methods with signatures that accept long millseconds are much harder to track.

This implementation uses a ScheduledThreadPoolExecutor to track one time validation methods. It sets the mIsVibrating flag to false just after a one time vibration method finishes.

publicclassManagedVibrator {

    publicstaticfinalStringTAG= ManagedVibrator.class.getSimpleName();

    private Context mContext;
    private Vibrator mVibrator;
    privatebooleanmIsVibrating=false;
    private ScheduledThreadPoolExecutor mExecutor;

    privateRunnablemVibrationEndRunnable=newRunnable() {
        @Overridepublicvoidrun() {
            setVibrating(false);
        }
    };

    publicManagedVibrator(Context context) {
        this.mContext = context;
        mVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
        mExecutor = newScheduledThreadPoolExecutor(1);
    }

    publicbooleanhasVibrator() {
        return mVibrator.hasVibrator();
    }

    publicvoidvibrate(long milliseconds) {
        setVibrating(true);
        mVibrator.vibrate(milliseconds);
        notifyOnVibrationEnd(milliseconds);
    }

    // Requires API v21publicvoidvibrate(long milliseconds, AudioAttributes attributes) {
        setVibrating(true);
        mVibrator.vibrate(milliseconds, attributes);
        notifyOnVibrationEnd(milliseconds);
    }

    publicvoidvibrate(long[] pattern, int repeat) {
        setVibrating(true);
        mVibrator.vibrate(pattern, repeat);
    }

    // Requires API v21publicvoidvibrate(long[] pattern, int repeat, AudioAttributes attributes) {
        setVibrating(true);
        mVibrator.vibrate(pattern, repeat, attributes);
    }

    publicvoidcancel() {
        mVibrator.cancel();
        setVibrating(false);
    }

    publicbooleanisVibrating() {
        return mIsVibrating;
    }

    privatevoidsetVibrating(boolean isVibrating) {
        mIsVibrating = isVibrating;
    }

    privatevoidnotifyOnVibrationEnd(long milliseconds) {
        try {
            mExecutor.schedule(mVibrationEndRunnable, milliseconds, TimeUnit.MILLISECONDS);
        } catch (RejectedExecutionException e) {
            Log.e(TAG, e.getMessage());
        }
    }

}

Usage

ManagedVibratorvibrator=newManagedVibrator(this);
vibrator.vibrate(5000);
...
if (vibrator.isVibrating()) {
    // Do something
}

Limitations

  1. You need to use one ManagedVibrator instance in your application
  2. ManagedVibrator can only tell you about vibrations initiated by your application. It does not know anything about vibrations caused by other applications of services.
  3. Very long vibration times or very frequent one time vibrations may cause issues

Solution 3:

There's a bad way for rooted phones but you will get atleast something.

You can read the file at:

"/sys/class/timed_output/vibrator/enable"

It stores the time left as microseconds when the device is vibrating. (tried on 5.1)

For unrooted phones, you can just check "dumpsys vibrator"Process output with a BufferedReader. It's updated once the vibrator finishes vibrating.

Post a Comment for "Detect The Device Is Vibrating?"