Device vibration detection?

I used the code below to vibrate the device.

public void vibrator() { try { Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(5000); } catch (Exception e) { Log.d(TAG, "vibrator exception: " + e); } } 

Can we programmatically detect this event (check that the device vibrates)?

+5
source share
3 answers

No, you can’t.

The same question is here .

You can check if device vibration is supported:

 Vibrator mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); boolean hasVibrator = mVibrator.hasVibrator(); 

More details:

+2
source

There's a bad way for root phones, but you get at least something.

You can read the file at:

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

Saves the time remaining for a microsecond when the device vibrates. (verified 5.1)

For idle phones, you can simply check the "dumpsys vibrator" Process with the BufferedReader . It is updated after vibrating the vibrator.

0
source

Introduction

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

Managedvibrator

The following is the ManagedVibrator class, which is a wrapper for the Vibrator class. All Vibrator methods are included with the optional isVibrating() method.

constant vibration methods with signatures that accept a long[] pattern are easily tracked because cancel() must be called to stop the vibration. However, one-time vibration methods with signatures that accept long millseconds are much more difficult to track.

This implementation uses the ScheduledThreadPoolExecutor to track one-time validation methods . It sets the mIsVibrating flag to false immediately after the completion of the one-time vibration method .

 public class ManagedVibrator { public static final String TAG = ManagedVibrator.class.getSimpleName(); private Context mContext; private Vibrator mVibrator; private boolean mIsVibrating = false; private ScheduledThreadPoolExecutor mExecutor; private Runnable mVibrationEndRunnable = new Runnable() { @Override public void run() { setVibrating(false); } }; public ManagedVibrator(Context context) { this.mContext = context; mVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); mExecutor = new ScheduledThreadPoolExecutor(1); } public boolean hasVibrator() { return mVibrator.hasVibrator(); } public void vibrate(long milliseconds) { setVibrating(true); mVibrator.vibrate(milliseconds); notifyOnVibrationEnd(milliseconds); } // Requires API v21 public void vibrate(long milliseconds, AudioAttributes attributes) { setVibrating(true); mVibrator.vibrate(milliseconds, attributes); notifyOnVibrationEnd(milliseconds); } public void vibrate(long[] pattern, int repeat) { setVibrating(true); mVibrator.vibrate(pattern, repeat); } // Requires API v21 public void vibrate(long[] pattern, int repeat, AudioAttributes attributes) { setVibrating(true); mVibrator.vibrate(pattern, repeat, attributes); } public void cancel() { mVibrator.cancel(); setVibrating(false); } public boolean isVibrating() { return mIsVibrating; } private void setVibrating(boolean isVibrating) { mIsVibrating = isVibrating; } private void notifyOnVibrationEnd(long milliseconds) { try { mExecutor.schedule(mVibrationEndRunnable, milliseconds, TimeUnit.MILLISECONDS); } catch (RejectedExecutionException e) { Log.e(TAG, e.getMessage()); } } } 

Using

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

Limitations

  • You need to use one instance of ManagedVibrator in your application.
  • ManagedVibrator can only inform you of vibrations triggered by your application. He does not know anything about vibrations caused by other service applications.
  • Very long vibrations or very frequent vibrations at the same time can cause problems.
-1
source

All Articles