Easy Tutorial
❮ Android Tutorial Gestures Android Tutorial Tablelayout ❯

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:

>


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

Follow on WeChat

❮ Android Tutorial Gestures Android Tutorial Tablelayout ❯