Android long-pressed vibration instance

Source: Internet
Author: User

Code first

Package COM. test; import android. app. activity; import android. app. service; import android. OS. bundle; import android. OS. vibrator; import android. view. view; import android. view. view. onlongclicklistener; import android. widget. textview; public class againtestactivity extends activity implements onlongclicklistener {private textview news;/** called when the activity is first created. * // @ override public void on Create (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); findviews (); setlistner ();} private void setlistner () {NEWS. setonlongclicklistener (this);} private void findviews () {NEWS = (textview) findviewbyid (R. id. newsword) ;}@ overridepublic Boolean onlongclick (view v) {Switch (v. GETID () {case R. id. newsword:/** vibration Service */vibrator VIB = (vibrator) This. getsystemser Vice (service. vibrator_service); // vibrator. vibrate (1000); // only vibrate for one second, one long [] pattern = {}; // two parameters, one is custom vibration mode, // The meanings of numbers in the array are static duration, vibration duration, static duration, and vibration duration... The unit of duration is millisecond // The second parameter is "whether to vibrate repeatedly".-1 does not vibrate repeatedly // The second parameter must be smaller than the pattern length. Otherwise, arrayindexoutofboundsexceptionvib will be thrown. vibrate (pattern, 1); break; default: break;} return false ;}}

Of course, in the registration file, add <uses-Permission Android: Name = "android. Permission. Vibrate"/>

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.