您的位置:首页 > 其它

手机震动实现

2013-04-11 15:58 876 查看
1.手机震动的实现

比如静音时出现震动,360垃圾清理有个动画效果,接收短信提示,游戏皮球碰撞,贪吃蛇碰到墙壁等等。震动事件的控制是通过控制震动时间周期来表现出差异。

//获取振动器

1. Vibrator mVibrator = (Vibrator).getApplication().getSystemService(Service.VIBRATOR_SERVICE);

//控制震动周期,repeat=-1时,震动周期只出现一次;repeat=0时,震动周期一直持续,必须手动取消

2. mVibrator.vibrate(long milliseconds..., int repeat);


在AndroidManifest文件中添加震动权限:[b]<uses-permission android:name="android.permission.VIBRATE" />。[/b]

@SuppressLint("NewApi")

private void callVibrate() {

// TODO Auto-generated method stub

Log.i("xiabing", "message");

//vibrator.vibrate(100);

//通过设置震动的时间大小使我们可以感觉到震动的差异

vibrator.vibrate(new long[] { 100, 10, 100, 1000,1000, 50, 1000, 50, 1000}, 0);


//例子,在程序起动后等待0.1秒后,振动0.01秒,再等待0.1秒后,振动1秒,再等待1秒后,振动0.05秒

}

private void cancelVibrate() {

// TODO Auto-generated method stub

vibrator.cancel();

}

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