10.4 Vibrator
Category Android Basic Tutorial
Introduction to This Section:
>
In this section, we introduce the Vibrator, which is the built-in vibrator of the mobile phone. Don't search for "needle vibrator" directly on Baidu, because your search results may be the mysterious props shown in the picture, or other mysterious props:
Well, back to the Vibrator introduced in this section, it is actually a service provided by Android for the phone to vibrate! For example, in our previous Notification, we can set the vibration, when we receive push messages, we can set the vibration reminder, it is essential for games, such as the "airplane shooting" game, when your plane is shot down, it will vibrate for a long time!
Next, let's write a simple example to familiarize ourselves with the usage of this Vibrator!
Official API documentation: Vibrator
1. Obtain a Vibrator instance:
Vibrator vb = (Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
2. Related methods that can be used:
>
abstract void cancel (): Turn off or stop the vibrator
abstract boolean hasVibrator (): Determine whether the hardware has a vibrator
void vibrate (long milliseconds): Control the phone to vibrate for milliseconds milliseconds
void vibrate (long[] pattern,int repeat): Specify the phone to vibrate in the pattern specified by pattern! For example: if the pattern is new int[200,400,600,800], it will alternately turn on and off the vibrator at 200, 400, 600, 800 this time! And the second is the number of repetitions, if it is -1, it will vibrate only once, if it is 0, it will vibrate continuously There are two other methods that are not used much~ By the way, using the vibrator also requires adding the following permission in AndroidManifest.xml: <uses-permission android:name="android.permission.VIBRATE" />
3. Usage Example: Set a vibrator with different frequencies:
>
The most widely used Vibrator is undoubtedly the so-called mobile phone massager app. When searching in the app market, there are a lot of them. The author casually downloaded a few to take a look, and they are all similar. This little gadget actually has more than 80,000 downloads... Well, it doesn't seem to be much, But the common function is to switch the vibration frequency to complete, and the so-called massage effect, whether it is really effective is unknown, So next, let's implement a simple massager! The core is actually: the array parameter in vibrate(), just write an array according to your own needs! The following code needs to be tested on a real machine!
Running effect picture :
Implementation code :
A simple layout file, five buttons: activity_main.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_hasVibrator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check if there is a vibrator" />
<Button
android:id="@+id/btn_short"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Short Vibration" />
<Button
android:id="@+id/btn_long"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Long Vibration" />
<Button
android:id="@+id/btn_rhythm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rhythmic Vibration" />
<Button
android:id="@+id/btn_cancle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel Vibration" />
</LinearLayout>
Then the MainActivity.java part:
``` public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_hasVibrator;
private Button btn_short;
private Button btn_long;
private Button btn_rhythm;
private Button btn_cancle;
private Vibrator myVibrator;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Get the
1.8 Project Related Analysis (Various Files, Resource Access)
2.5.4 Basic Usage of AutoCompleteTextView (Auto-Complete Text Box)
[2.5.6 Basic Usage of ViewFlipper (Flip View)
5.2.4 Fragment Instance Detailed Lecture - Bottom Navigation Bar + ViewPager Slide Switching Pages
5.2.5 Fragment Instance Detailed Lecture - News (Shopping) App List Fragment Simple Implementation
6.1 Data Storage and Access - File Storage Reading and Writing
6.2 Data Storage and Access - SharedPreferences to Save User Preference Parameters
6.3.1 Data Storage and Access - First Look at SQLite Database
6.3.2 Data Storage and Access - Another Look at SQLite Database
7.1.1 Android Network Programming Essentials and Http Protocol Learning
8.3.4 Paint API - Xfermode and PorterDuff Detailed Explanation (I)
8.3.5 Paint API - Xfermode and PorterDuff Detailed Explanation (II)
8.3.6 Paint API - Xfermode and PorterDuff Detailed Explanation (III)
8.3.7 Paint API - Xfermode and PorterDuff Detailed Explanation (IV)
8.3.8 Paint API - Xfermode and PorterDuff Detailed Explanation (V)
[8.3.10 Paint API - ColorFilter (Color Filter
12.4 DrySister Viewing Girls App (First Edition) — 4. Adding Data Caching (Integrating SQLite)