android震动控制

1.添加权限
 <uses-permission android:name="android.permission.VIBRATE"/>
1.获得Vibrator实例
 Vibrator vb = (Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
2.可以使用的相关方法
    //关闭或者停止振动器
    abstract void cancel():
    //判断硬件是否有振动器
    abstract boolean hasVibrator()
    //控制手机振动为milliseconds毫秒
    void vibrate(long milliseconds)
    //指定手机以pattern指定的模式振动!比如:pattern为new int[200,400,600,800],
    //就是让他在200,400,600,800这个时间交替启动与关闭振动器! 而第二个则是重复次数,
    //如果是-1的只振动一次,如果是0的话则一直振动
    void vibrate(long[] pattern,int repeat): 
3.例:
private Vibrator vibrator;
vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
tbSwitchVibrator.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                long [] pattern = {500,1000,500,1000};   // 停止 开启 停止 开启
                vibrator.vibrate(pattern,-1);           //重复两次上面的pattern 如果只想震动一次,index设为-1
            }else {
                vibrator.cancel();
            }
        }
    });
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,253评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,489评论 25 709
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,999评论 6 342
  • 2017年3月26,人生就是这么奇妙,昨天我们的美女老师说如果有时间一定要带大家去穿越生死,让大家去体验死亡到重生...
    莲与心阅读 168评论 0 0
  • 夏天的热,让我们更加怀念冬天的冷,尤其是下雪天,飘飘雪花满天飞舞,到处是一片银白。那真是:北国风光,千里冰封,万里...
    仉咏阅读 271评论 0 5