Android implements the method of calling vibrations _android

Source: Internet
Author: User

This article illustrates the way Android realizes calling Vibrations. Share to everyone for your reference, specific as follows:

Call the Android system vibration, only need a class that is vibrator, this class in the hard package, a look at the system-level services, but also through the Manifest.xml file set permissions

<?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 learn the SDK together.

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

The vibrator class is used to manipulate vibrations on the device, and if your thread exits, the starting vibrations will stop.

public void vibrate (long[] pattern, int repeat)
SINCE:API Level 1

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

Pass in a array of ints that are the durations for which to turn on or off the vibrator in milliseconds. The ' a ' of 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 in before turning it off. Subsequent values alternate between durations in milliseconds to turn the "vibrator off" and "to" turn the vibrator on.

Passes an integer array as the duration, in milliseconds, for closing and opening vibrations. The first value indicates the number of milliseconds to wait for the vibration to open, and the next value indicates the number of milliseconds to hold the vibration, alternating the number of milliseconds that the vibration is closed and opened.

To cause the "repeat", pass the "index into the" pattern array in which to start the repeat, or-1 to disable Repea Ting.
In order to repeat according to the set rhythm vibration, pass the index parameter to indicate the number of repetitions, with-1 means not repeat.

Parameters
Pattern a array of longs of times for which to turn the vibrator on or off.
Repeat the "index into" which to repeat and or-1 if you don ' t want to repeat.

It also contains a method called Cancel, which is used to cancel the vibration.

Look at a demo of the Code:

*
 * @author octobershiner
 * SE. HIT
 * A demo * * * * * *
package uni.vibrator using Android phone vibration;
Import android.app.Activity;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.os.Vibrator;
public class Vibratordemoactivity extends activity {
  private vibrator vibrator;
  /** called the activity is a. * *
  @Override public
  void OnCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.main);
     * * To set the vibration size can be changed by changing pattern to set, if the opening time is too short, the vibration effect may not feel
     * * * * *
    vibrator = (vibrator) Getsystemservice ( Context.vibrator_service);
    Long [] pattern = {100,400,100,400}; Stop opening stop opening
    vibrator.vibrate (pattern,2);//Repeat two times above pattern if you only want to vibrate once, index set to-1
  } public
  void OnStop () {
    super.onstop ();
    Vibrator.cancel ();
  }

I hope this article will help you with the Android program.

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.