SoFunction
Updated on 2025-04-09

Android programming method to realize mobile phone vibration function

This article describes the method of Android programming to implement mobile phone vibration function. Share it for your reference, as follows:

When interacting with users, vibrating function is often used to remind users. This function is relatively simple to implement, please refer to the main code below:

import ;
import ;
import ;
public class TipHelper {
  public static void Vibrate(final Activity activity, long milliseconds) {
    Vibrator vib = (Vibrator) (Service.VIBRATOR_SERVICE);
    (milliseconds);
  }
  public static void Vibrate(final Activity activity, long[] pattern,booleanisRepeat) {
    Vibrator vib = (Vibrator) (Service.VIBRATOR_SERVICE);
    (pattern, isRepeat ? 1 : -1);
  }
}

Of course, the above code is not possible. We also need to add vibration permissions in :

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

Through the above operations, we can use the functions defined by TipHelper. The parameters of the two Vibrate functions are briefly introduced as follows:

final Activity activity: Activity instance that calls this method

long milliseconds: The vibration time is milliseconds

long[] pattern
: Custom vibration mode. The meaning of numbers in the array is the duration of static, vibration, static, vibration. . . The unit of duration is milliseconds

boolean isRepeat : Whether it vibrates repeatedly, if it is true, vibrates repeatedly, if it is false, it will only vibrate once

PS: For file-related attribute functions, please refer to the online tools of this site:

Android Manifest function and permission description:
http://tools./table/AndroidManifest

For more information about Android related content, please check out the topic of this site:Android multimedia operation skills summary (audio, video, recording, etc.)》、《Android resource operation skills summary》、《Android programming activity operation skills summary》、《Android development introduction and advanced tutorial》、《Android View View Tips Summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.