Android basics tutorial-10.4 Vibrator (Vibrator)

Source: Internet
Author: User

Android basics tutorial-10.4 Vibrator (Vibrator)
 

This section introduces:

This section describesVibrator(Vibrator), is the vibrator that comes with the mobile phone, do not go to Baidu to directly search for the vibrator, because
Your search results may be mysterious items, or other mysterious items:

Well, the Vibrator introduced in this section is actually a service provided by Android for body vibration!
For example, we can set the vibration in the previous Notification. When receiving the PUSH message, we can set the vibration.
Reminder, a game is essential, such as a "airplane fight" game. When your plane explodes, it will shake you!
Let's write a simple example to familiarize yourself with the usage of this Vibrator!
Official API documentation: Vibrator

1. Obtain the Vibrator instance:

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

2. Available Methods:
Abstract void Cancel(): Close or stop the vibrator abstract boolean HasVibrator(): Determines whether the hardware has a vibrator void. Vibrate(Long milliseconds): controls the cell phone vibration to milliseconds millisecond void Vibrate(Long [] pattern, int repeat): specifies that the mobile phone vibrates in the pattern specified by pattern!
For example, if pattern is new int [200,400,600,800], it is used to enable or disable the vibrator at 200,400,600,800!
The second one is the number of repetitions. If it is-1, it only vibrates once. If it is 0, it always vibrates.
The other two methods are not used much ~
By the way, you also need to add the following permissions to AndroidManifest. xml to use the vibrator:
< Uses-permission android: name = "android. permission. VIBRATE"/>
3. Example: Set the vibrator with different frequencies:

The most widely used Vibrator app is the so-called mobile phone massager app, which can be searched in the app market.
A few times later, they are all the same, this little thing actually has more than 8 million downloads... Well, it seems that there are not many,
However, the common function is to switch the vibration frequency. It is unknown whether the so-called massage effect is effective,
Then let's implement a simple massager!
The core is actually the parameter of the array in vibrate (). You can write an array as needed!
The following code needs to be tested on a real machine!

Run:

Implementation Code:

Simple layout file, five buttons:Activity_main.xml:


  
   
   
   
   
  

NextMainActivity. javaPart:

Public class MainActivity extends AppCompatActivity implements View. onClickListener {private Button listener; private Button btn_short; private Button btn_long; private Button listener; private Button btn_cancle; private Vibrator myVibrator; private Context mContext; @ Override protected void onCreate (Bundle listener) {super. onCreate (savedInstanceState); setContentView (R. layout. Activity_main); // obtain the system's Vibrator instance: 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_cancl E = (Button) findViewById (R. id. btn_cancle); listener (this); btn_listener .setonclicklistener (this); btn_long.setOnClickListener (this); listener (this) ;}@ Override public void onClick (View v) {switch (v. getId () {case R. id. btn_hasVibrator: Toast. makeText (mContext, myVibrator. hasVibrator ()? Current device has vibrator: current device has no 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 vibration, Toast. LENGTH_SHORT ). show (); break; case R. id. btn_long: myVibrator. cancel (); myVibrator. vibrate (new long [] {100,100,100,100 0}, 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; case R. id. btn_cancle: myVibrator. cancel (); Toast. makeText (mContext, cancel vibration, Toast. LENGTH_SHORT ). show ();}}}

By the way, do not miss the vibrator permission!


  
 

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.