Android This system provides service--vibrator (vibrator)
--Reprint Please specify source: Coder-pig
Vibrator Simple introduction and related methods:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvy29kzxjfcgln/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
simple demo--Set frequency different Vibrators
The most widely used for vibrator is the so-called mobile phone Massager class app, a search in the app market, a bunch, the author casually down a few down to look at
, it's all the same, this little thing. Incredibly, there are more than 8W downloads ... Well, it doesn't seem like much, just the general function is to switch the vibration frequency to complete
The so-called massage effect, whether really effective is unclear, then the next
Let's implement a simple massager!
The core is actually the vibrate () in the array of the parameters, according to their own needs to write an array can be!
Because the simulator does not vibrate, so the need to perform on the phone will be effective Oh!
:
The code is also very easy, the layout of the words of four simple button just
Activity_main.xml:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:id=" @+id/linearlayout1 "android:layout_width=" Match_parent "Android:layout_hei ght= "match_parent" android:orientation= "vertical" tools:context= "com.jay.example.vibratordemo.MainActivity" > <button android:id= "@+id/btnshort" android:layout_width= "wrap_content" android:layout_height= "wrap _content "android:text=" short vibration "/> <button android:id=" @+id/btnlong "android:layout_width=" wrap _content "android:layout_height=" wrap_content "android:text=" long Vibration "/> <button android:id= "@+id/btnrhythm" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:tex t= "Rhythm Vibration"/> <button android:id= "@+id/btncancle" android:layout_width= "Wrap_content" Android:layout_height= "Wrap_content" android:text= "Remove Vibration"/> </LinearLayout>
then the mainactivity is written, here and the previous section is the same way, let the activity class implement the Onclicklistener interface
Overriding the OnClick method, depending on the ID of the view can infer which button clicked, the advantage is that the component is more
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 btnrhythm;private button btncancel; Private vibrator myvibrator; @Overrideprotected void 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);//Vibrator instance of obtaining 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,1000}, 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 (), "suppress vibration", toast.length_short). Show ();}}
Finally, don't forget to add permission to the Androidmanifest.xml file!
<uses-permission android:name= "Android.permission.VIBRATE"/>
Well, the basic use of the method is actually very easy, there is not much to say, and the above also said that the virtual machine is no vibration effect, so
need to publish the application to the mobile phone ability to detect the effect! Reference code Download: Vibratordemo.rar
For the convenience of you, directly to the APK export, directly download the installation to the phone can test the effect, of course, is only a small demo, will not
push ads, hair SMS What =-=! install to be able to see what permissions are required!apk download: vibratordemo.apk
Android This system provides service--vibrator (vibrator)