手机震动的节奏 Vibrator

↘锁芯ラ 提交于 2020-02-19 06:29:31

 1 package com.turboradio.googlesdk; 2  3 import android.app.Activity; 4 import android.app.Service; 5 import android.os.Bundle; 6 import android.os.Vibrator; 7 import android.view.View; 8 import android.widget.Toast; 9 import android.widget.ToggleButton;10 11 public class Ex5_6 extends Activity {12     private Vibrator vibrator;13     private ToggleButton myToggleButton1;14     private ToggleButton myToggleButton2;15     private ToggleButton myToggleButton3;16     @Override17     protected void onCreate(Bundle savedInstanceState) {18         super.onCreate(savedInstanceState);19         setContentView(R.layout.ex_5_6);20         vibrator = (Vibrator)getApplication().getSystemService(Service.VIBRATOR_SERVICE);21         myToggleButton1 = (ToggleButton)findViewById(R.id.myToggleButton1);22         myToggleButton2 = (ToggleButton)findViewById(R.id.myToggleButton2);23         myToggleButton3 = (ToggleButton)findViewById(R.id.myToggleButton3);24     }25     /**短震动**/26     public void toggleButton1Listener(View v){27         if(myToggleButton1.isChecked()){28             /**设置震动的周期**/29             vibrator.vibrate(new long[]{100,10,100,100,1000}, -1);30             Toast.makeText(Ex5_6.this, "短震动中..", Toast.LENGTH_LONG).show();31         }else{32             /**取消震动**/33             vibrator.cancel();34             Toast.makeText(Ex5_6.this, "震动结束!!", Toast.LENGTH_LONG).show();35         }36     }37     /**长震动**/38     public void toggleButton2Listener(View v){39         if(myToggleButton2.isChecked()){40             vibrator.vibrate(new long []{100,100,100,1000}, 0);41             Toast.makeText(Ex5_6.this, "长震动...", Toast.LENGTH_LONG).show();42         }else{43             /**取消震动**/44             vibrator.cancel();45             Toast.makeText(Ex5_6.this, "长震动结束", Toast.LENGTH_LONG).show();46         }47     }48     /**有节奏震动**/49     public void toggleButton3Listener(View v){50         if(myToggleButton3.isChecked()){51             vibrator.vibrate(new long[]{1000,50,1000,50}, 0);52             Toast.makeText(Ex5_6.this, "有节奏震动", Toast.LENGTH_LONG).show();53         }else{54             vibrator.cancel();55             Toast.makeText(Ex5_6.this, "有节奏震动结束", Toast.LENGTH_LONG).show();56         }57     }58 }
 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="fill_parent" 4     android:layout_height="fill_parent" 5     android:orientation="vertical" > 6     <ToggleButton  7         android:id="@+id/myToggleButton1" 8         android:layout_width="wrap_content" 9         android:layout_height="wrap_content"10         android:text="短震动"11         android:onClick="toggleButton1Listener"12         />13     <ToggleButton 14         android:id="@+id/myToggleButton2"15         android:layout_width="wrap_content"16         android:layout_height="wrap_content"17         android:textOn="长震动"18         android:onClick="toggleButton2Listener"19         />20     <ToggleButton 21         android:id="@+id/myToggleButton3"22         android:layout_width="wrap_content"23         android:layout_height="wrap_content"24         android:text="节奏震动"25         android:onClick="toggleButton3Listener"26         />27 </LinearLayout>

<uses-permissionandroid:name="android.permission.VIBRATE"/>



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!