Android开发之手机震动器

Android中用Vibrator类的对象来控制震动器。

要获得设备的震动器,要调用getSystemService(String)函数,以VIBRATOR_SERVICE 为参数

       Vibrator类有以下成员函数:

Public Methods
abstract void cancel()
Turn the vibrator off.
abstract boolean hasVibrator()
Check whether the hardware has a vibrator.
abstract void vibrate(long[] pattern, int repeat)
Vibrate with a given pattern.
abstract void vibrate(long milliseconds)
Vibrate constantly for the specified period of time.
调用vibrate方法来产生震动,根据传递的参数不同,有两种震动方式。

如果只传递一个long参数,这个参数用来指定振动的毫秒数,如要震动5秒,则按如下方式调用:

vibrator.vibrate(5000);

如果传递多个参数,震动器就按照给定的模式震动,震动模式由数组pattern指定。如要按以下方式震动:

等待1秒,震动2秒,等待1秒,震动3秒,则把数组设置如下:

long[] pattern = {1000200010003000};

第二个参数为-1表示不重复, 如果不是-1, 比如改成1, 表示从前面这个long数组的下标为1的元素开始重复(这个说法应该靠谱,而不是重复的次数,因为当用这个数组的时候传4过去,会出现下图情况)



完整的代码如下:

package com.peng.hello.activity;

import android.app.Activity;
import android.app.Service;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.TextView;

public class HelloActivity extends Activity 
{
    Vibrator vibrator;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    	vibrator = (Vibrator)getApplication().getSystemService(Service.VIBRATOR_SERVICE);
    }
    
    public void onMyClick(View view)
    {
    	long[] pattern = {800, 50, 400, 30}; // OFF/ON/OFF/ON...   
    	vibrator.vibrate(pattern, 4);
    	Toast.makeText(this, "button click", Toast.LENGTH_SHORT).show();
//    	vibrator.cancel();
    }
}
以上程序要添加一个按钮, onMyClick 为按钮的响应程序。在mail.xml中添加如下程序:

<Button
        android:id="@+id/Btm01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:onClick="onMyClick"></Button>

记得在AndroidManifest.xml文件添加权限,如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.peng.hello.activity"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".HelloActivity"
            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>
    <uses-permission android:name="android.permission.VIBRATE" /> 
</manifest>
注,以上程序红色部分为添加进去的。


还有程序要在真机上运行才能有震动的效果,模拟器上不支持震动的。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值