Allows android buttons to vibrate the key sound.

Source: Internet
Author: User

Allows android buttons to vibrate the key sound.

To call the system vibration service when pressing a key, you must first add the corresponding permissions in AndroidMainfest. xml.

Then Vibrator vibrator = (Vibrator) getSystemService (VIBRATOR_SERVICE );
// Vibrator. vibrate (3000 );
// Set the Vibrate vibration cycle
Vibrator. vibrate (new long [] {1000,2000, 3000,4000}, 0 );

Here, I found a method class for writing good vibrations on the Internet.

 

Package com. lxb. switchdemo; import android. app. activity; import android. app. service; import android. OS. vibrator; import android. widget. linearLayout;/*** mobile phone vibration tool class ** @ author Administrator **/public class VibratorUtil {/*** final Activity: the activity instance that calls this method long milliseconds: vibration duration, in milliseconds * long [] pattern: 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 * boolean isRepeat: whether to Vibrate repeatedly. If it is true, it vibrates repeatedly. If it is false, it only shakes once */public static void Vibrate (final Activity, long milliseconds) {Vibrator vib = (Vibrator) activity. getSystemService (Service. VIBRATOR_SERVICE); vib. vibrate (milliseconds);} public static void Vibrate (final Activity activity, long [] pattern, boolean isRepeat) {Vibrator vib = (Vibrator) activity. getSystemService (Service. VIBRATOR_SE RVICE); vib. vibrate (pattern, isRepeat? 1:-1 );}}
Of course, you only need to add

 

VibratorUtil. Vibrate (Switch_demoActivity.this, 100 );

A simple vibration mechanism can be implemented.
 

Next, we will summarize the implementation of the button sound mechanism,

 

  1. Public class MainActivity extends Activity {
  2. Private Button mButton01;
  3. Private SoundPool sp; // declare a SoundPool
  4. Private int music; // defines an integer using load (); To set suondID
  5.  
  6. @ Override
  7. Public void onCreate (Bundle savedInstanceState ){
  8.  
  9. Super. onCreate (savedInstanceState );
  10. SetContentView (R. layout. activity_main );
  11. MButton01 = (Button) findViewById (R. id. mButton01 );
  12. Sp = new SoundPool (10, AudioManager. STREAM_SYSTEM, 5); // The first parameter is the maximum number of data streams simultaneously played, the second data stream type, and the third parameter is the sound quality.
  13. Music = sp. load (this, R. raw. start, 1); // put your audio material in res/raw. The first parameter is the resource file, and the second parameter is the music priority.
  14.  
  15. MButton01.setOnClickListener (new OnClickListener (){
  16. @ Override
  17. Public void onClick (View v ){
  18. Sp. play (music, 1, 1, 0, 0, 1 );
  19. }
  20. }
  21. }
  22. } Raw is a new folder under res, which is a resource file that can be directly used without compilation. Of course, to avoid the problem that keys cannot be recognized in some machines, it is best to convert the audio format to ogg.

     

     

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.