In interaction with users, the vibration function is often used to remind users. This function is easy to implement. See the following code:
[Java] import android. app. Activity;
Import android. app. Service;
Import android. OS. Vibrator;
Public class TipHelper {
Public static void Vibrate (final Activity activity, long milliseconds ){
Vibrator vib = (Vibrator) activity. getSystemService (Service. VIBRATOR_SERVICE );
Vib. vibrate (milliseconds );
}
Public static void Vibrate (final Activity activity, long [] pattern, boolean isRepeat ){
Vibrator vib = (Vibrator) activity. getSystemService (Service. VIBRATOR_SERVICE );
Vib. vibrate (pattern, isRepeat? 1:-1 );
}
}
Import android. app. Activity;
Import android. app. Service;
Import android. OS. Vibrator;
Public class TipHelper {
Public static void Vibrate (final Activity activity, long milliseconds ){
Vibrator vib = (Vibrator) activity. getSystemService (Service. VIBRATOR_SERVICE );
Vib. vibrate (milliseconds );
}
Public static void Vibrate (final Activity activity, long [] pattern, boolean isRepeat ){
Vibrator vib = (Vibrator) activity. getSystemService (Service. VIBRATOR_SERVICE );
Vib. vibrate (pattern, isRepeat? 1:-1 );
}
}
Of course, www.2cto.com only has the above Code. We also need to add the vibration permission to AndroidManifest. xml:
[Html] <uses-permission android: name = "android. permission. VIBRATE"/>
<Uses-permission android: name = "android. permission. VIBRATE"/>
Through the above operation, we can use the function defined by TipHelper. The parameters of the two Vibrate functions are described as follows:
Final Activity: the activity instance that calls this method
Long milliseconds: the duration of the vibration, in milliseconds
Long [] pattern: Custom vibration mode. The meanings of numbers in the array are [static duration, vibration duration, static duration, and vibration duration]. The duration is measured in milliseconds.
Boolean isRepeat: whether to vibrate repeatedly. If it is true, it shakes repeatedly. If it is false, it only shakes once.
From the pure soul