具体的方法如下
方法 | 类型 | 描述 |
public voidcancel() | 普通 | 取消震动 |
public booleanhasVibrator() | 普通 | 判断是否震动 |
public voidvibrate(long[] pattern, int repeat) | 普通 | 设置震动周期,如果repeat为-1则不循环震动 |
public voidvibrate(long milliseconds) | 普通 | 打开震动 |
首先启动一个震动服务:
private Vibator vubator =null;
vibator=(Vibrator) super.getApplication().getSystemService(Service.VIBRATOR_SERVICE); // 取得震动服务
然后是设置监听事件;
this.myChronometer.setOnChronometerTickListener(new OnChronometerTickListenerImpl());实现监听方法
private class OnChronometerTickListenerImplimplements OnChronometerTickListener{@Overridepublic void onChronometerTick(Chronometer chronometer){String time =chronometer.getText().toString().replaceAll("[^(\d{2}:\d{2})]", ""); // 取出时间if ("01:00".equals(time)) { // 满一分钟MyChronometerDemo.this.vibrator.vibrate(new long[] { 1000,10,1000, 100}, 0);// 设置震动周期,以及循环震动
————————————————————————————————————————————————————
方法说明:
public void vibrate(long[] pattern, intrepeat)
参数中的long型数组传入:第一个是等待多长事件开始震动,第二个是每次震动多长事件,以毫秒为单位。
intrepeat为重复震动的次数,如果为0则为持续循环,如果为-1则不重复震动。
publicvoid vibrate(long milliseconds)
设置开启震动。
public voidcancel()
取消一个震动操作。
public boolean hasVibrator()
.XML 文件
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > android:id="@+id/timer" android:layout_width="wrap_content" android:layout_height="wrap_content" /> |
package com.example.chronometer; import android.os.Bundle; import android.os.SystemClock; import android.os.Vibrator; import android.app.Activity; import android.app.Service; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; import android.widget.Chronometer.OnChronometerTickListener; public class MainActivity extends Activity { private Button start=null; private Button stop=null; private Chronometer timer=null; private Button restart=null; private Button format=null; private Vibrator vibrator = null ; @Override protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_main); this.start=(Button)super.findViewById(R.id.start); this.stop=(Button)super.findViewById(R.id.stop); this.timer=(Chronometer)super.findViewById(R.id.timer); this.start.setOnClickListener(newStartTimer()); this.stop.setOnClickListener(newStopTimer()); this.restart=(Button)super.findViewById(R.id.restart); this.format=(Button)super.findViewById(R.id.format); this.restart.setOnClickListener(newRestartTimer()); this.format.setOnClickListener(newFormatTimer()); this.timer.setFormat("%s"); this.timer.setOnChronometerTickListener(newTimerListener()); this.vibrator = (Vibrator)super.getApplication().getSystemService( Service.VIBRATOR_SERVICE); } private class StartTimer implements OnClickListener{ @Override public voidonClick(View v) { MainActivity.this.timer.start(); }} private class StopTimer implements OnClickListener{ @Override public voidonClick(View v) { MainActivity.this.timer.stop(); }} private class RestartTimer implements OnClickListener{ @Override public voidonClick(View v) { MainActivity.this.timer.setBase(SystemClock.elapsedRealtime()); }} private class FormatTimer implements OnClickListener{ @Override public voidonClick(View v) { MainActivity.this.timer.setFormat(" 格式化显示:%s"); }} private class TimerListener implementsOnChronometerTickListener{ @Override public voidonChronometerTick(Chronometer chronometer) { String time =chronometer.getText().toString() .replaceAll("[^(\d{2}:\d{2})]", ""); if ("00:10".equals(time)) { MainActivity.this.vibrator.vibrate(new long[] {1000, 10000, 1000, 100 }, 0); if ("00:20".equals(time)) { MainActivity.this.vibrator.cancel(); } }} } } |