Android Introductory Tutorial Vibrator (vibrator) _android

Source: Internet
Author: User

Objective:

Vibrator Introduction:

Let's write a simple example to familiarize ourselves with the use of this vibrator!

1. Obtain Vibrator Examples:

Vibrator vb = (vibrator) getsystemservice (Service.vibrator_service);

2. The relevant methods that can be used:

1.stract void Cancel (): Turn off or stop the vibrator

2.tract boolean Hasvibrator (): Determine if the hardware has a vibrator

3.id vibrate (long milliseconds): Control phone vibration for milliseconds milliseconds

4.id vibrate (long[] Pattern,int repeat): Specifies the mode of the mobile phone to vibrate with pattern specified! For example: pattern for new int[200,400,600,800], is let him in 200,400,600,800 this time alternately start and close the vibrator! The second one is the number of repetitions, if it is-1 of the only vibration once, if it is 0 then the vibration and two other methods used not much ~ right, using the vibrator also needs to add the following permissions in the Androidmanifest.xml:<uses-permission android:name="android.permission.VIBRATE"/>

3. Use Example: Set the frequency of a different vibrator:

The most widely used for vibrator is the so-called mobile phone Massager type of app, in the app market a search, a pile, the author casually down a few down to see, are the same, this little gadget unexpectedly has 8W more download quantity ... Well, it seems not too much, but the general function is to switch the frequency of vibration to complete, and the so-called massage effect, whether it is really effective on the unknown, then we will come to realize a simple massage! The core is actually: vibrate () in the array of parameters, according to their own needs to write an array on it! The following code needs to be tested on the real machine!

Run Effect chart:

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


  Ator "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:text=" to determine whether there is a vibrator "/> <button android:id= "@+id/btn_short" android:layout_width= wrap_content "android:layout_height=" Wrap_cont
    Ent "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" Andr Oid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Rhythm Vibration"/> <Button A Ndroid:id= "@+id/btn_cancle" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:t ext= "Cancel Vibration"/> </linearlAyout> 

followed by the Mainactivity.java section:

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);
    Obtain the vibrator instance of the system: 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.ma Ketext (Mcontext, Myvibrator.hasvibrator ()?
        "Current device has vibrator": "Current equipment without vibrator", Toast.length_short). Show ();
      Break
        Case R.id.btn_short:myvibrator.cancel ();
        Myvibrator.vibrate (new long[]{100, 200, 100, 200}, 0);
        Toast.maketext (Mcontext, "Short Vibrations", Toast.length_short). Show ();
      Break
        Case R.id.btn_long:myvibrator.cancel ();
        Myvibrator.vibrate (new long[]{100, 100, 100, 1000}, 0);
        Toast.maketext (Mcontext, "long Vibration", 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, "Rhythm Vibration", toast.length_short). Show ();
      Break CaseR.id.btn_cancle:myvibrator.cancel ();
    Toast.maketext (Mcontext, "Cancel Vibration", Toast.length_short). Show (); }
  }
}

That's right, don't miss the vibrator permission Oh!

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

Summary:

Well, this article we learned the basic use of vibrator (vibrator), the code is very simple, not to quickly add to your app, let your application hi up ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.