How to vibrate the phone for Windows Phone 8

July 21, 2014

Applies to: Windows Phone 8 and Windows Phone Silverlight 8.1 | Windows Phone OS 7.1

Windows Phone devices include a vibration controller. Your app can vibrate the phone for up to 5 seconds to notify the user of an important event. This topic describes how to vibrate the phone.

Use the vibration feature in moderation. Do not rely on the vibration feature for critical notifications, because the user can disable vibration.

To test an app that uses the vibration controller effectively, you have to test it on a physical device. The emulator cannot simulate vibration and does not provide any visual feedback that vibration is occurring.

An app that is running in the background cannot vibrate the phone. If your code tries to use vibration while the app is running in the background, nothing happens, but no exception is raised. If you want to vibrate the phone while your app is running in the background, you have to implement a toast notification. For more info, see Toasts for Windows Phone 8.

This topic contains the following sections.

You have to specify a valid value for the duration argument of the method that starts the vibration. If you specify a value less than 0 or greater than 5 seconds for the duration of the vibration, an ArgumentException is thrown.

You vibrate the phone with the .NET API by calling the Start(TimeSpan) method of the VibrateController class.

  1. Import the Microsoft.Devices namespace.

    using Microsoft.Devices;
    
  2. Get a reference to the vibration controller by using the static Default property of the VibrateController class.

    VibrateController testVibrateController = VibrateController.Default;
    
  3. Start the vibration by calling the Start(TimeSpan) method of the VibrateController class. Specify the duration as a TimeSpan value.

    testVibrateController.Start(TimeSpan.FromSeconds(3));
    
  4. If necessary, stop the vibration by calling the Stop() method of the VibrateController class.

    testVibrateController.Stop();
    

You vibrate the phone with the Windows Phone Runtime API by calling the Vibrate method of the VibrationDevice class.

  1. Import the Windows.Phone.Devices.Notification namespace.

    using Windows.Phone.Devices.Notification;
    
  2. Get a reference to the vibration controller by calling the static GetDefault method of the VibrationDevice class.

    VibrationDevice testVibrationDevice = VibrationDevice.GetDefault();
    
  3. Start the vibration by calling the Vibrate method of the VibrationDevice class. Specify the duration as a TimeSpan value.

    testVibrationDevice.Vibrate(TimeSpan.FromSeconds(3));
    
  4. If necessary, stop the vibration by calling the Cancel method of the VibrationDevice class.

    testVibrationDevice.Cancel();
    

Show:
© 2014 Microsoft