|
方法
|
类型
|
描述
|
|
public voidcancel()
|
普通
|
取消震动
|
|
public booleanhasVibrator()
|
普通
|
判断是否震动
|
|
public voidvibrate(long[] pattern, int repeat)
|
普通
|
设置震动周期,如果repeat为-1则不循环震动
|
|
public voidvibrate(long milliseconds)
|
普通
|
打开震动
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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" >
<Chronometer
android:id="@+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/timer"
android:text="开始" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/timer"
android:layout_toRightOf="@id/start"
android:text="结束" />
<Button
android:id="@+id/restart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/timer"
android:layout_toRightOf="@id/stop"
android:text="复位" />
<Button
android:id="@+id/format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/timer"
android:layout_toRightOf="@id/restart"
android:text="格式化" />
</RelativeLayout>
.JAVA文件
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
protected void 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(new StartTimer());
this.stop.setOnClickListener(new StopTimer());
this.restart=(Button)super.findViewById(R.id.restart);
this.format=(Button)super.findViewById(R.id.format);
this.restart.setOnClickListener(new RestartTimer());
this.format.setOnClickListener(new FormatTimer());
this.timer.setFormat("%s");
this.timer.setOnChronometerTickListener(new TimerListener());
this.vibrator = (Vibrator) super.getApplication().getSystemService(
Service.VIBRATOR_SERVICE);
}
private class StartTimer implements OnClickListener{
@Override
public void onClick(View v) {
MainActivity.this.timer.start();
}}
private class StopTimer implements OnClickListener{
@Override
public void onClick(View v) {
MainActivity.this.timer.stop();
}}
private class RestartTimer implements OnClickListener{
@Override
public void onClick(View v) {
MainActivity.this.timer.setBase(SystemClock.elapsedRealtime() );
}}
private class FormatTimer implements OnClickListener{
@Override
public void onClick(View v) {
MainActivity.this.timer.setFormat(" 格式化显示:%s");
}}
private class TimerListener implements OnChronometerTickListener{
@Override
public void onChronometerTick(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();
}
}}
}
}
原文:http://blog.csdn.net/u013616976/article/details/20069399