How to make an Android device vibrate?

后端 未结 13 2005
长发绾君心
长发绾君心 2020-11-22 15:47

I wrote an Android application. Now, I want to make the device vibrate when a certain action occurs. How can I do this?

13条回答
  •  鱼传尺愫
    2020-11-22 16:31

    Above answer is very correct but I'm giving an easy step to do it:

     private static final long[] THREE_CYCLES = new long[] { 100, 1000, 1000,  1000, 1000, 1000 };
    
      public void longVibrate(View v) 
      {
         vibrateMulti(THREE_CYCLES);
      }
    
      private void vibrateMulti(long[] cycles) {
          NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
          Notification notification = new Notification();
    
          notification.vibrate = cycles; 
          notificationManager.notify(0, notification);
      }
    

    And then in your xml file:

    
    

    That's the easiest way.

提交回复
热议问题