SoFunction
Updated on 2025-04-09

android vibration and prompt sound implementation code

Android message prompts (vibration and prompt sound), the implementation code is as follows:

public class VibratorUtil {
  protected AudioManager audioManager;
  protected Vibrator vibrator;
  private Ringtone ringtone;
  private static final int MIN_TIME_OUT = 4000; //Time interval  long lastNotificationTime;
  public VibratorUtil() {
    audioManager = (AudioManager) ().getSystemService(Context.AUDIO_SERVICE); //This method is called by Context    vibrator = (Vibrator) ().getSystemService(Context.VIBRATOR_SERVICE); //Same as above  }
  /**
    * Turn on the phone vibration and play the system prompt ringtone
    */
  public void vibrateAndPlayTone() {
    if (() - lastNotificationTime < MIN_TIME_OUT) {
      return;
    }
    try {
      lastNotificationTime = ();
      if (() == AudioManager.RINGER_MODE_SILENT) {
        return;
      }
      long[] pattern = new long[]{0, 180, 80, 120};
      (pattern, -1); //shock      if (ringtone == null) {
        Uri notificationUri = (RingtoneManager.TYPE_NOTIFICATION);
        ringtone = ((), notificationUri);
        if (ringtone == null) {
          return;
        }
      }
      if (!()) {
        ();
        //Judge mobile phone brand        String vendor = ;
        if (vendor != null && ().contains("samsung")) {
          Thread ctlThread = new Thread() {
            public void run() {
              try {
                (3000);
                if (()) {
                  ();
                }
              } catch (Exception e) {
              }
            }
          };
          ();
        }
      }
    } catch (Exception e) {
      ();
    }
  }
}

Summarize

The above is the implementation code of Android vibration and prompt sound introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!
If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!