[Android Note 4] Android call vibration example

Source: Internet
Author: User

I did some other work in the past two days because I used the arraylist class when I used the getproviders () method yesterday. I suddenly wanted to have a deep understanding of its mechanism, and I got a lot of results, I read the source code of the Java Collection framework analyzed by a great god, so that the Fail fast mechanism is naked. The core code in the middle takes me into the door of the design pattern, the original source code uses the decorator design mode, today I also summarized the learning results in http://blog.csdn.net/octobershiner/article/details/6631997

To put it bluntly, to call the Android system, only one class is vibrator. In the hard package, you need to set the permissions through the manifest. xml file to view system-level services.

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="uni.vibrator"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="8" />    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".VibratorDemoActivity"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>     <uses-permission android:name="android.permission.VIBRATE" /></manifest>

Let's take a look at the SDK.

Class that operates the vibrator on the device.
If your process exits, any vibration you started with will stop.

// The Vibrator class is used to operate the vibration on the device. If your thread exits, the starting vibration will also stop.

Certificate ---------------------------------------------------------------------------------------------------------------------------------------------------
Public void vibrate (long [] pattern, int repeat)
Since: API Level 1

Vibrate with a given pattern. // vibrate according to the given rhythm

Pass in an array of ints that are the durations for which to turn on or off the vibrator in milliseconds. the first value indicates the number of milliseconds to wait before turning the vibrator on. the next value indicates the number of milliseconds for which to keep the vibrator on before turning it off. subsequent values alternate between durations in milliseconds to turn the vibrator off or to turn the vibrator on.
// Pass an integer array as the duration for disabling and enabling the vibration, in milliseconds. The first value indicates the number of milliseconds to wait for the vibration to start. The next value indicates the number of milliseconds to keep the vibration.

To cause the pattern to repeat, pass the index into the pattern array at which to start the repeat, or-1 to disable repeating.
// In order to repeatedly vibrate at the set rhythm, the index parameter is passed to indicate the number of repetitions, and the-1 parameter is used to indicate non-repetition.

Parameters
Pattern an array of longs of times for which to turn the vibrator on or off.
Repeat the index into pattern at which to repeat, or-1 if you don't want to repeat.
Certificate ---------------------------------------------------------------------------------------------------------------------------------------------------

It also contains a method called Cancel to cancel the vibration.

DEMO code

/** @ Author octobershiner * 2011 7 25 * se. hit * a demo using Android mobile phone shake **/package uni. vibrator; import android. app. activity; import android. content. context; import android. OS. bundle; import android. OS. vibrator; public class vibratordemoactivity extends activity {private vibrator;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main);/** you can change the pattern to set the vibration size. If the enabling time is too short, the vibration effect may not feel **/vibrator = (vibrator) getsystemservice (context. vibrator_service); long [] pattern = {100,400,100,400}; // stop enabling and stop enabling vibrator. vibrate (pattern, 2); // repeat the above pattern twice. If you only want to shake it once, the index is set to-1} public void onstop () {super. onstop (); vibrator. cancel ();}}

I don't know how to simulate it on a virtual machine, so I have to test it on a real machine first. If I know how to simulate it on a simulator, I 'd like to tell you about it ~

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.