Vibrator(vibrator)


Introduction to this section:

In this section we introduce Vibrator (vibrator), which is the vibrator that comes with the mobile phone. Don’t search directly on Baidu needle vibrator because Your search results may be the mysterious prop 1.gif as shown in the picture, or other mysterious props:

Well, back to the Vibrator introduced in this section, it is actually provided by Android for the fuselage A shocking service! For example, we can set a vibration in our Notification earlier. When we receive a push message, we can set a vibration. Reminder, games are essential, such as the "jerk off" game. When your airplane is blown up, it will vibrate!

Let’s write a simple example to get familiar with the usage of this Vibrator!

Official API document: Vibrator


1. Obtain 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's vibration to milliseconds milliseconds
  • void vibrate(long[] pattern,int repeat): Specify the phone to vibrate in the pattern specified by pattern! For example: pattern is new int[200,400,600,800], which means that it alternately starts and turns off the vibrator at the time of 200,400,600,800! The second one is the number of repetitions. If it is -1, it will only vibrate once. If it is 0, it will vibrate all the time. There are two other methods that are rarely used~ By the way, using the vibrator also requires adding the following permissions to AndroidManifest.xml: <uses-permission android:name="android.permission.VIBRATE"/>

##3. Usage example: Set vibrations with different frequencies Device:

The most widely used app for Vibrator is the so-called mobile massager app. After searching in the app market, there were a lot of them, and I downloaded them casually. After taking a look at them, they are all similar. This gadget has more than 80,000 downloads...well, it doesn't seem to be too much. However, common functions are accomplished by switching the vibration frequency, and it is not known whether the so-called massage effect is really effective. So let’s implement a simple massager next! The core is actually: the parameters of the array in vibrate(), just write an array according to your needs! The following code needs to be tested on a real machine!

Running renderings:

2.gif

Implementation code:

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


    <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>

Then comes 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);
        //获得系统的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 permissions!

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

4. Sample code download:

VibratorDemo.zip


Summary of this section:

Okay, in this section we have learned the basic use of Vibrator (vibrator). The code is very simple, so hurry up and join in. In your APP, make your application HI~, well, that’s all, thank you. It’s getting late, so Xiaozhu should go home quickly! After all, I am still a young girl! It would be bad if you get wet~3.gif