您的位置:首页 > 移动开发 > Android开发

android--手机震动--Vibrator

2012-08-31 16:49 387 查看
android.os.Vibrator代表这手机的振动器,是通过this.getSystemService(Service.VIBRATOR_SERVICE)获取的,然后通过用vibrate来实现震动,用cancel方法来取消震动。

1、获取震动器的实例

Vibrator vibrator = (Vibrator) this.getSystemService(Service.VIBRATOR_SERVICE);

2、设置震动时长,单位是ms

vibrator.vibrate(2500);

3、也可以设置有节奏的

vibrator.vibrate(new long[]{10, 10, 50, 50, 50}, -1);

注意:-1代表着震动不重复

4、使用振动器必须要在AndroidManifest.xml中指定权限

<uses-permission android:name="android.permission.VIBRATE"/>



实例如下:

1、布局文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

>

<ToggleButton

android:id="@+id/tb1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:checked="false"

android:textOff="@string/vibrate"

android:textOn="@string/cancel" />

<TextView

android:id="@+id/tv1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/vibrateoff"

/>

</LinearLayout>

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

>

<ToggleButton

android:id="@+id/tb2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:checked="false"

android:textOff="@string/vibrate"

android:textOn="@string/cancel" />

<TextView

android:id="@+id/tv2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/vibrateoff"

/>

</LinearLayout>

</LinearLayout>

2、代码:

package com.phoneVibrate;

import android.app.Activity;

import android.app.Service;

import android.os.Bundle;

import android.os.Vibrator;

import android.widget.CompoundButton;

import android.widget.CompoundButton.OnCheckedChangeListener;

import android.widget.TextView;

import android.widget.ToggleButton;

public class PhoneVibrateActivity extends Activity {

Vibrator vibrator;

TextView tv1;

TextView tv2;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

tv1 = (TextView) findViewById(R.id.tv1);

tv2 = (TextView) findViewById(R.id.tv2);

vibrator = (Vibrator) this.getSystemService(Service.VIBRATOR_SERVICE);

ToggleButton tb1 = (ToggleButton) findViewById(R.id.tb1);

tb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if(isChecked){

vibrator.vibrate(new long[]{10, 10, 50, 50, 50}, -1);

tv1.setText(R.string.vibrateon);

}else{

vibrator.cancel();

tv1.setText(R.string.vibrateoff);

}

}

});

ToggleButton tb2 = (ToggleButton) findViewById(R.id.tb2);

tb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {



@Override

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if(isChecked){

vibrator.vibrate(2500);



tv2.setText(R.string.vibrateon);

}else{

vibrator.cancel();

tv2.setText(R.string.vibrateoff);

}

}

});

}

}

3、AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.phoneVibrate"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk android:minSdkVersion="15" />

<uses-permission android:name="android.permission.VIBRATE"/>

<application

android:icon="@drawable/ic_launcher"

android:label="@string/app_name" >

<activity

android:name=".PhoneVibrateActivity"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: