歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android-Vibrator的使用

Android-Vibrator的使用

日期:2017/3/1 10:55:35   编辑:Linux編程

Android手機中的震動由Vibrator實現。設置震動事件,需要知道其震動的時間長短、震動的周期等。

在android中,震動的時間一毫秒計算(1/1000秒),所以如果設置的時間值太小,會感覺不出來。

通過調用Vibrator的vibrate(long[] pattern, int repeat)方法實現。

前一個參數為設置震動的效果的數組,第二個參數為 -1表示只震動一次,為0則震動會一直持續。

一個demo:

  1. package com.shao.vibrator;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.os.Vibrator;
  5. import android.widget.CompoundButton;
  6. import android.widget.Toast;
  7. import android.widget.CompoundButton.OnCheckedChangeListener;
  8. import android.widget.ToggleButton;
  9. public class VibratorActivity extends Activity {
  10. /** Called when the activity is first created. */
  11. private Vibrator vibrator;
  12. private ToggleButton tog1;
  13. private ToggleButton tog2;
  14. private ToggleButton tog3;
  15. @Override
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.main);
  19. init();
  20. tog1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  21. @Override
  22. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  23. // TODO Auto-generated method stub
  24. if(isChecked){
  25. //設置震動周期
  26. vibrator.vibrate(new long[]{1000,10,100,1000}, -1);
  27. showToast("OK");
  28. }else{
  29. //取消震動
  30. vibrator.cancel();
  31. showToast("CANCEL");
  32. }
  33. }
  34. });
  35. tog2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  36. @Override
  37. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  38. // TODO Auto-generated method stub
  39. if(isChecked){
  40. //設置震動周期
  41. vibrator.vibrate(new long[]{100,100,100,1000}, 0);
  42. showToast("OK");
  43. }else{
  44. //取消震動
  45. vibrator.cancel();
  46. showToast("CANCEL");
  47. }
  48. }
  49. });
  50. tog3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  51. @Override
  52. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  53. // TODO Auto-generated method stub
  54. if(isChecked){
  55. //設置震動周期
  56. vibrator.vibrate(new long[]{1000,50,1000,50,1000}, 0);
  57. showToast("OK");
  58. }else{
  59. //取消震動
  60. vibrator.cancel();
  61. showToast("CANCEL");
  62. }
  63. }
  64. });
  65. }
  66. private void init(){
  67. tog1= (ToggleButton) findViewById(R.id.tog1);
  68. tog2= (ToggleButton) findViewById(R.id.tog2);
  69. tog3= (ToggleButton) findViewById(R.id.tog3);
  70. vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
  71. }
  72. private void showToast(String msg){
  73. Toast.makeText(this, msg, 1).show();
  74. }
  75. }
xml:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <RelativeLayout
  8. android:layout_marginTop="20dp"
  9. android:orientation="horizontal"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content">
  12. <TextView
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="短震動"
  16. />
  17. <ToggleButton
  18. android:id="@+id/tog1"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:textOn="關閉"
  22. android:textOff="打開"
  23. android:layout_alignParentRight="true"
  24. />
  25. </RelativeLayout>
  26. <RelativeLayout
  27. android:layout_marginTop="20dp"
  28. android:orientation="horizontal"
  29. android:layout_width="fill_parent"
  30. android:layout_height="wrap_content">
  31. <TextView
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:text="長震動"
  35. />
  36. <ToggleButton
  37. android:id="@+id/tog2"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:textOn="關閉"
  41. android:textOff="打開"
  42. android:layout_alignParentRight="true"
  43. />
  44. </RelativeLayout>
  45. <RelativeLayout
  46. android:layout_marginTop="20dp"
  47. android:orientation="horizontal"
  48. android:layout_width="fill_parent"
  49. android:layout_height="wrap_content">
  50. <TextView
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:text="節奏震動"
  54. />
  55. <ToggleButton
  56. android:id="@+id/tog3"
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:textOn="關閉"
  60. android:textOff="打開"
  61. android:layout_alignParentRight="true"
  62. />
  63. </RelativeLayout>
  64. </LinearLayout>

最後別忘了加上 <uses-permission android:name="android.permission.VIBRATE"/> 權限
Copyright © Linux教程網 All Rights Reserved