Vibrator手机震动服务

机构android.os.Vibrator
具体的方法如下
方法
类型
描述
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{
@Override
public 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文件

<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();
				
			}
		
	}}
}
}



Vibrator手机震动服务,布布扣,bubuko.com

Vibrator手机震动服务

上一篇:在Ecilpse中集成Vim-like编辑器vrapper


下一篇:Android测试原理(三)——使用Eclipse的ADT进行测试