Farmers in yards world, beautiful application experience, from the programmer for the processing of detail, and self requirements state, agriculture in the yard a member of the young people is busy, every day, every week, can leave some footprints, is the creation of content, there is a persistent, is I don’t know why, if you lost, might as well to Chou Chou code track of farmers.
If you are interested, you can follow the public account Biglead for more content to share.
In some business scenarios of mobile phone App, such as the user clicks a button to operate the vibration prompt or the scan code in the process of successful vibration prompt the user, is a good detail processing, this article to share with you the code implementation in Android.
1 Add permissions to the listing file
Add permissions to the manifest file androidmanifest.xml as follows
<uses-permission android:name="android.permission.VIBRATE" />
Copy the code
2 Obtain system services
Get the Vibrator in your Activity:
import android.os.Vibrator;
private Vibrator mVibrator;
// Vibration effect system service
mVibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
Copy the code
3. Vibration prompt effect
If the code scan is successful in service application scenarios, the user is prompted by a vibration:
/* * Mode of vibration */
// vibrator.vibrate(2000); // Vibrate for two seconds
// Below is the vibration that can make the vibration regular -1: does not repeat 0: cycle vibration
long[] pattern = {200.200 };
mVibrator.vibrate(pattern, -1);
Copy the code
For pattern, if defined as new int[200,400,600,800]
-
200: indicates the number of milliseconds to wait before turning on the vibrator. 0.2 seconds.
-
400: indicates the number of milliseconds to keep the vibrator on before turning it off, 0.4 seconds.
-
600,800: Alternate between a few milliseconds to turn off the vibrator or turn on the vibrator, 0.6-0.8 seconds.
4. Talk more
The Vibrator, which comes with your phone, is a service provided by Android for vibration. For example, we can set up vibration reminder when mobile phone receives push message. Or login page function, the user input password or user name error, you can give a prompt and then shake.
// Turn off or stop the vibrator
mVibrator.cancel();
Copy the code
// Check whether vibration is supported
mVibrator.hasVibrator()
Copy the code