1、计时器最基本的4个方法 .start() 开始计时 .stop() 停止计时 .setBase(SystemClock.elapsedRealtime()); 复位 myChronometer.setFormat("新的显示格式:%s。"); 格式化。
2、设置闹铃
package com.jackie.chronometerproject;
import android.app.Activity;
import android.app.Service;
import android.os.Bundle;
import android.os.SystemClock;
import android.os.Vibrator;
import android.util.Log;
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 MyDemo extends Activity {
private Chronometer myChronometer=null;
private Button myBut1=null;
private Button myBut2=null;
private Vibrator vibrator=null; //定义振动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.myChronometer=(Chronometer) super.findViewById(R.id.myChronometer);
this.myBut1=(Button) super.findViewById(R.id.myBut1);
this.myBut2=(Button) super.findViewById(R.id.myBut2);
this.vibrator=(Vibrator)super.getApplication().getSystemService(Service.VIBRATOR_SERVICE); //获取手机震动的服务
this.myChronometer.setFormat("当前计时时间:s%."); //格式化
this.myBut1.setOnClickListener(new OnClickListenerImplStart());
this.myBut2.setOnClickListener(new OnClickListenerImplStop());
this.myChronometer.setOnChronometerTickListener(new OnChronometerTickListenerImpl()); //设置监听时间改变函数
}
private class OnChronometerTickListenerImpl implements OnChronometerTickListener{
@Override
public void onChronometerTick(Chronometer arg0) {
String time=MyDemo.this.myChronometer.getText().toString();
Log.i("当前的时间", time);
if("00:30".equals(time)){
MyDemo.this.vibrator.vibrate(new long[]{1000,10,100,100}, 0); //设置震动频率和是否循环震动 等待1s,震动0.01s,再等待0.1s,再震动0.1s }
}
}
private class OnClickListenerImplStart implements OnClickListener{
@Override
public void onClick(View arg0) {
myChronometer.start();
}
}
private class OnClickListenerImplStop implements OnClickListener{
@Override
public void onClick(View arg0) {
myChronometer.stop();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_demo, menu);
return true;
}
}
import android.app.Activity;
import android.app.Service;
import android.os.Bundle;
import android.os.SystemClock;
import android.os.Vibrator;
import android.util.Log;
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 MyDemo extends Activity {
private Chronometer myChronometer=null;
private Button myBut1=null;
private Button myBut2=null;
private Vibrator vibrator=null; //定义振动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.myChronometer=(Chronometer) super.findViewById(R.id.myChronometer);
this.myBut1=(Button) super.findViewById(R.id.myBut1);
this.myBut2=(Button) super.findViewById(R.id.myBut2);
this.vibrator=(Vibrator)super.getApplication().getSystemService(Service.VIBRATOR_SERVICE); //获取手机震动的服务
this.myChronometer.setFormat("当前计时时间:s%."); //格式化
this.myBut1.setOnClickListener(new OnClickListenerImplStart());
this.myBut2.setOnClickListener(new OnClickListenerImplStop());
this.myChronometer.setOnChronometerTickListener(new OnChronometerTickListenerImpl()); //设置监听时间改变函数
}
private class OnChronometerTickListenerImpl implements OnChronometerTickListener{
@Override
public void onChronometerTick(Chronometer arg0) {
String time=MyDemo.this.myChronometer.getText().toString();
Log.i("当前的时间", time);
if("00:30".equals(time)){
MyDemo.this.vibrator.vibrate(new long[]{1000,10,100,100}, 0); //设置震动频率和是否循环震动 等待1s,震动0.01s,再等待0.1s,再震动0.1s }
}
}
private class OnClickListenerImplStart implements OnClickListener{
@Override
public void onClick(View arg0) {
myChronometer.start();
}
}
private class OnClickListenerImplStop implements OnClickListener{
@Override
public void onClick(View arg0) {
myChronometer.stop();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_demo, menu);
return true;
}
}
3、系统授权给震动服务
<uses-permission
android:name="android.permission.VIBRATE"/>
android:name="android.permission.VIBRATE"/>