Section Introduction:

In this section, what we introduce is theVibrator(Vibrator), which is the phone's built-in vibrator. Don't go directly to Baidu and search for vibrators, because your search results may be the mysterious prop shown in the figure, or other mysterious props:

Well, back to the Vibrator introduced in this section, it is actually a service provided by Android for the device to vibrate! For example, in the earlier Notification we can set vibration. When a push message is received, we can set a vibration reminder. It is essential for games, such as the "Fighting Planes" game. When your plane is blown up by someone, it will vibrate for a long time!

Now let's write a simple example to get familiar with the usage of the Vibrator!

Official API documentation:Vibrator


1. Obtain the Vibrator instance:


Vibrator vb = (Vibrator)getSystemService(Service.VIBRATOR_SERVICE);


2. Related available methods:

  • 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 the length of time specified by `milliseconds`
  • void vibrate(long[] pattern, int repeat): Specify the phone to vibrate according to the mode specified by pattern! For example: if pattern is new int[200,400,600,800], it means letting the vibrator alternately start and stop at the time intervals 200, 400, 600, 800! The second parameter is the repeat count; if it is -1, it vibrates only once; if it is 0, it keeps vibrating. There are also two other methods that are not commonly used. By the way, using the vibrator also requires adding the following permission in AndroidManifest.xml: <uses-permission android:name="android.permission.VIBRATE"/>

3. Example usage: Setting a vibrator with different frequencies:

For Vibrator, the most widely used application is the so-called phone massager apps. Searching in the app market, there are a lot. The author casually downloaded a few to look at, and they are all much the same. This little thing actually has over 80,000 downloads... Well, it doesn't seem like a lot. But the common feature is to switch the vibration frequency to complete the massage, and whether the so-called massage effect is really effective is unknown. Next, let's implement a simple massager! The core is actually: the array parameter in vibrate(); you just need to write an array according to your own needs! The following code needs to be tested on a real device!

Running effect screenshot:

Implementation code:

Simple layout file with 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="判断是否有振动器" />


    <Button
        android:id="@+id/btn_short"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="短振动" />

    <Button
        android:id="@+id/btn_long"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="长振动" />

    <Button
        android:id="@+id/btn_rhythm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="节奏振动" />

    <Button
        android:id="@+id/btn_cancle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="取消振动" />
</LinearLayout>

Next is theMainActivity.javapart:

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);
        //获得系统的Vibrator实例:
        myVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
        mContext = MainActivity.this;
        bindViews();
    }

    private void bindViews() {
        btn_hasVibrator = (Button) findViewById(R.id.btn_hasVibrator);
        btn_short = (Button) findViewById(R.id.btn_short);
        btn_long = (Button) findViewById(R.id.btn_long);
        btn_rhythm = (Button) findViewById(R.id.btn_rhythm);
        btn_cancle = (Button) findViewById(R.id.btn_cancle);

        btn_hasVibrator.setOnClickListener(this);
        btn_short.setOnClickListener(this);
        btn_long.setOnClickListener(this);
        btn_rhythm.setOnClickListener(this);
        btn_cancle.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_hasVibrator:
                Toast.makeText(mContext, myVibrator.hasVibrator() ? "当前设备有振动器" : "当前设备无振动器",
                        Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_short:
                myVibrator.cancel();
                myVibrator.vibrate(new long[]{100, 200, 100, 200}, 0);
                Toast.makeText(mContext, "短振动", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_long:
                myVibrator.cancel();
                myVibrator.vibrate(new long[]{100, 100, 100, 1000}, 0);
                Toast.makeText(mContext, "长振动", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_rhythm:
                myVibrator.cancel();
                myVibrator.vibrate(new long[]{500, 100, 500, 100, 500, 100}, 0);
                Toast.makeText(mContext, "节奏振动", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_cancle:
                myVibrator.cancel();
                Toast.makeText(mContext, "取消振动", Toast.LENGTH_SHORT).show();
        }
    }
}

By the way, don't miss the vibrator permission!

<uses-permission android:name="android.permission.VIBRATE"/>

4. Example code download:

VibratorDemo.zip


Section summary:

Okay, in this section we learned the basic usage of the Vibrator. The code is very simple. Why not add it to your APP right away and make your application HI~. Well, that's all for now. Thank you. It's getting late, the little pig should hurry home! After all, I am still an innocent young lad! It would be bad if I got wet~