Vibrator, a system service provided by Android)
Vibrator, a system service provided by Android)
-- Reprinted with the source: coder-pig
Vibrator introduction and related methods:
Simple demo -- set frequencyDifferent Vibrator
The most widely used app for Vibrator is the so-called mobile phone massager app, which is searched and piled up in the app market. I just took a few moments to look at it.
These are all similar.There are more than 8 million downloads... well, it seems that there are not too many downloads, but the general function is to switch the vibration frequency to complete.
It is unknown whether the so-called massage effect is effective.
Let's implement a simple massager!
The core is actually the parameter of the array in vibrate (). You can write an array as needed!
Because the simulator does not vibrate, it must be run on the mobile phone to achieve the effect!
:
The Code is also very simple. There are only four simple buttons for layout.
Activity_main.xml:
The next step is to write MainActivity, which is the same as the previous section. Let the Activity class implement the OnClickListener interface.
The onClick method can be rewritten to determine which button is clicked based on different view IDs. The advantage of this method is that there are many components.
This can simplify the code!
MainActivity. java:
Package com. jay. example. vibratordemo; import android. app. activity; import android. app. service; import android. OS. bundle; import android. OS. vibrator; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; public class MainActivity extends Activity implements OnClickListener {private Button btnshort; private Button btnlong; private Button extensions; private Button btncancel; private Vibrator myVibrator; @ jsonvoid onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btnshort = (Button) findViewById (R. id. btnshort); btnlong = (Button) findViewById (R. id. btnlong); btnrhythm = (Button) findViewById (R. id. btnrhythm); btncancel = (Button) findViewById (R. id. btncancle); btnshort. setOnClickListener (this); btnlong. setOnClickListener (this); btnrhythm. setOnClickListener (this); btncancel. setOnClickListener (this); // obtain the Vibrator instance of the system: myVibrator = (Vibrator) getSystemService (Service. VIBRATOR_SERVICE) ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. btnshort: myVibrator. cancel (); myVibrator. vibrate (new long [] {100,200,100,200}, 0); Toast. makeText (getApplicationContext (), "short vibration", Toast. LENGTH_SHORT ). show (); break; case R. id. btnlong: myVibrator. cancel (); myVibrator. vibrate (new long [] {100,100,100,100 0}, 0); Toast. makeText (getApplicationContext (), "Long vibration", Toast. LENGTH_SHORT ). show (); break; case R. id. btnrhythm: myVibrator. cancel (); myVibrator. vibrate (new long [] {500,100,500,100,500,100}, 0); Toast. makeText (getApplicationContext (), "rhythm vibration", Toast. LENGTH_SHORT ). show (); break; case R. id. btncancle: myVibrator. cancel (); Toast. makeText (getApplicationContext (), "cancel vibration", Toast. LENGTH_SHORT ). show ();}}}
Do not forget to add permissions to the AndroidManifest. xml file!
Well, the basic usage is actually very simple. I will not talk about it here. I also mentioned that the virtual machine has no vibration effect, so
You need to publish the application to your mobile phone to check the effect!Download the reference code: vibratorDemo.rar
For your convenience, you can export the apk directly. You can test the effect by directly downloading and installing the apk on your mobile phone. Of course, it's just a small demo.
Push ads, send text messages or something =-=! You can view required permissions during installation! Apk download:VibratorDemo.apk