Android Basics Getting Started Tutorial--10.4 vibrator (Vibrator)
tags (space delimited): Android Basics Getting Started Tutorial
Introduction to this section:
This section we introduce is vibrator(vibrator), is the mobile phone comes with the vibrator, do not go to Baidu directly search needle vibrator, because
Your search results may be mysterious props, or other mysterious props:
Well, speaking back to this section of the vibrator, is actually Android to provide us with a service for the fuselage vibration!
For example, we can set the vibration in front of our notification, we can set the vibration when we receive the push message.
Remind, the game must, such as "Jerk Off" game, when your plane gives a person to blow the time, the President vibrates!
Let's write a simple example to familiarize yourself with the usage of this vibrator!
Official API Document: Vibrator
1. Get Vibrator Instances:
Vibrator vb = (vibrator) getsystemservice (Service.vibrator_service);
2. Relevant methods that can be used:
- abstract void Cancel(): Close or stop the vibrator
- Abstract Boolean hasvibrator(): Determine if the hardware has a vibrator
- void vibrate(long milliseconds): Controls phone vibration of milliseconds milliseconds
- void vibrate(long[] Pattern,int repeat): Specifies that the phone vibrates in the mode specified by pattern!
For example: pattern is new int[200,400,600,800], is to let him at 200,400,600,800 this time alternately start and close the vibrator!
And the second is the number of repetitions, if it is 1 of the vibration only once, if it is 0, it has been vibrating
There are two other ways not to use much of it ~
Yes, the use of vibrators also requires the following permissions to be added to the androidmanifest.xml:
<uses-permission android:name= "Android.permission.VIBRATE"/>
3. Use Example: Set a vibrator with different frequencies:
The most widely used for vibrator is the so-called mobile phone Massager class app, in the app market a search, a bunch, the author casually down
A few come down to look, are very much the same, this little thing unexpectedly has 8W more download volume ... Well, it doesn't seem like much,
But the general function is to switch the vibration frequency to complete, and the so-called massage effect, whether really effective is unclear,
Then let's implement a simple massager!
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 :
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="Judging if there is a vibrator"/> <button android:ID="@+id/btn_short"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"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"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"Android:text="Rhythm Vibration"/> <button android:ID="@+id/btn_cancle"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"Android:text="Remove Vibration"/></linearlayout>
Next is the Mainactivity.java section:
Public class mainactivity extends appcompatactivity implements View. Onclicklistener { PrivateButton Btn_hasvibrator;PrivateButton Btn_short;PrivateButton Btn_long;PrivateButton Btn_rhythm;PrivateButton btn_cancle;PrivateVibrator Myvibrator;PrivateContext Mcontext;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//Get a 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 ()) { CaseR.id.btn_hasvibrator:toast.maketext (Mcontext, Myvibrator.hasvibrator ()?"Current device has vibrators":"Current Device no vibrator", Toast.length_short). Show (); Break; CaseR.id.btn_short:myvibrator.cancel (); Myvibrator.vibrate (New Long[]{ -, $, -, $},0); Toast.maketext (Mcontext,"Short Vibration", Toast.length_short). Show (); Break; CaseR.id.btn_long:myvibrator.cancel (); Myvibrator.vibrate (New Long[]{ -, -, -, +},0); Toast.maketext (Mcontext,"Long Vibration", Toast.length_short). Show (); Break; CaseR.id.btn_rhythm:myvibrator.cancel (); Myvibrator.vibrate (New Long[]{ -, -, -, -, -, -},0); Toast.maketext (Mcontext,"Rhythm Vibration", Toast.length_short). Show (); Break; CaseR.id.btn_cancle:myvibrator.cancel (); Toast.maketext (Mcontext,"Remove Vibration", Toast.length_short). Show (); } }}
Oh, yes, don't miss the vibrator.
<uses-permission android:name="android.permission.VIBRATE"/>
4. Sample code Download:
Vibratordemo.zip
This section summarizes:
OK, this section we learned the basic use of vibrator (vibrator), the code is very simple, not to hurry to join
Your app, let your app hi up ~, well, say so much, thank you, not early, piglet or hurry home!
After all, I'm a yellow-and-fat girlfriend! In case of wet, it is not good ~
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Basics Getting Started Tutorial--10.4 vibrator (Vibrator)