class="tags" href="/tags/ShouJi.html" title=手机>手机振动器,这个一般是不响铃声的时候才用,不过我一下想到了PlayStation平台上面的"生化危机",当操作的主角被僵尸啃食的时候,手柄就会强烈振动,class="tags" href="/tags/ShouJi.html" title=手机>手机游戏的振动器是"天然"具备的条件,制作游戏的时候也可以用来营造真实感和紧张气氛
class="tags" href="/tags/ShouJi.html" title=手机>手机的振动器也是用getSystemService()方法获取,它有三个方法控制class="tags" href="/tags/ShouJi.html" title=手机>手机振动
void vibrate(long milliseconds) 控制class="tags" href="/tags/ShouJi.html" title=手机>手机振动millonseconds毫秒
void vibrate(long[] pattern,int repeat) 指定class="tags" href="/tags/ShouJi.html" title=手机>手机以pattern指定的模式振动.如指定pattern为new int[400,800,1200,1600],就是指定在400ms,800ms,1200ms,1600ms这些时间点交替启动,关闭class="tags" href="/tags/ShouJi.html" title=手机>手机振动器;其中repeat指定pattern数组索引,指定对pattern数组中从repeat索引开始的振动进行循环
void cancel() 关闭class="tags" href="/tags/ShouJi.html" title=手机>手机振动
class="language-java">package WangLi.Service.VibratorTest;
import class="tags" href="/tags/ANDROID.html" title=android>android.app.Activity;
import class="tags" href="/tags/ANDROID.html" title=android>android.app.Service;
import class="tags" href="/tags/ANDROID.html" title=android>android.os.Bundle;
import class="tags" href="/tags/ANDROID.html" title=android>android.os.Vibrator;
import class="tags" href="/tags/ANDROID.html" title=android>android.view.MotionEvent;
import class="tags" href="/tags/ANDROID.html" title=android>android.widget.Toast;
public class VibratorTest extends Activity {
Vibrator vibrator;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取系统的Vibrator服务
vibrator = (Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
}
//重写onTouchEvent方法,当用户触碰屏时触发该方法
public boolean onTouchEvent(MotionEvent event)
{
Toast.makeText(this, "class="tags" href="/tags/ShouJi.html" title=手机>手机振动", 5000).show();
//控制class="tags" href="/tags/ShouJi.html" title=手机>手机振动2秒
vibrator.vibrate(2000);
return super.onTouchEvent(event);
}
}
最后一定要加上振动器的权限
class="language-html"><uses-permission class="tags" href="/tags/ANDROID.html" title=android>android:name="class="tags" href="/tags/ANDROID.html" title=android>android.permission.VIBRATE"/>