main activity
package com.example.mtk81286.vibratortest1; import android.os.Vibrator; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.CompoundButton; import android.widget.ToggleButton; public class VibratorTestMainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { private Vibrator vibrator; private ToggleButton tb1; private ToggleButton tb2; private ToggleButton tb3; private ToggleButton tb4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_vibrator_test_main); vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE); tb1 = (ToggleButton) findViewById(R.id.vibr_1); tb2 = (ToggleButton) findViewById(R.id.vibr_2); tb3 = (ToggleButton) findViewById(R.id.vibr_3); tb4 = (ToggleButton) findViewById(R.id.vibr_4); tb1.setOnCheckedChangeListener(this); tb2.setOnCheckedChangeListener(this); tb3.setOnCheckedChangeListener(this); tb4.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // if(isChecked && vibrator.hasVibrator()){ if(isChecked){ if(buttonView == tb1){ vibrator.vibrate(new long[]{500,2000,5000,1000}, -1); } else if(buttonView == tb2){ //500ms off, 100ms on, 5000ms off, 5000ms on. from array[2] repeat vibrator.vibrate(new long[]{500,100,5000,5000}, 2); } else if(buttonView == tb3){ //vibrator.vibrate(new long[]{1000,50,2000,5000,1000}, 0); //vibrator.vibrate(new long[]{1000,50,2000,5000,1000}, 1); vibrator.vibrate(new long[]{1000,50,2000,5000,1000}, 2); } else if(buttonView == tb4){ vibrator.vibrate(5000); } } else { //close vibrator vibrator.cancel(); } } /* Vibrator vibrator = new SystemVibrator(); vibrator.vibrate(SHUTDOWN_VIBRATE_MS); */ }
说明:
case1:
vibrator.vibrate(new long[]{500,100,5000,5000}, 2); //第一次等500ms,震100ms, 等5000ms,震5000ms. 后面,从array[2]开始循环,等5000ms, 震5000ms,如此反复
case 2:
vibrator.vibrate(new long[]{1000,50,2000,5000,1000}, 0);//第一次等1s,震50ms,等2s,震5ms,等1s, repeat 0, 后面节奏跟第一次一样
vibrator.vibrate(new long[]{1000,50,2000,5000,1000}, 1);//第一次等1s,震50ms,等2s,震5ms,等1s, repeat 1,从array[1]开始,等50ms,震2s,等5s,震1s,如此反复
vibrator.vibrate(new long[]{1000,50,2000,5000,1000}, 2);//第一次等1s,震50ms,等2s,震5ms,等1s, repeat 2,从array[2]开始,等2s,震5s,等1s,剩下按这个节奏2,5,1来
///////////////////////////
xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.mtk81286.vibratortest1.VibratorTestMainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="单次震动" /> <ToggleButton android:id="@+id/vibr_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="开启" android:textOn="关闭"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="持续震动" /> <ToggleButton android:id="@+id/vibr_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="开启" android:textOn="关闭"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="间隔震动" /> <ToggleButton android:id="@+id/vibr_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="开启" android:textOn="关闭"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="定时长震动" /> <ToggleButton android:id="@+id/vibr_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="开启" android:textOn="关闭"/> </LinearLayout> </LinearLayout>
AndroidManifest.xml
//注意权限要加在<application>之前,且在其外面
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mtk81286.vibratortest1"> <uses-permission android:name="android.permission.VIBRATE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".VibratorTestMainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>