zoukankan      html  css  js  c++  java
  • Andorid手机振动器(Vibrator)的使用

    标签:

    android

    vibrator

    震动器

    it

    分类: Andorid

    获取振动器Vibrator实例: 
    Vibrator  mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    Vibrator.vibrate()方法:
    只有1个参数的时候,第一个参数用来指定振动的毫秒数。
    要传递2个参数的时候,第1个参数用来指定振动时间的样本,第2个参数用来指定是否需要循环。 
    振动时间的样本是指振动时间和等待时间的交互指定的数组。
     

    ※下面的例子,在程序起动后等待3秒后,振动1秒,再等待2秒后,振动5秒,再等待3秒后,振动1秒
            long[] pattern = {3000, 1000, 2000, 5000, 3000, 1000}; // OFF/ON/OFF/ON…

    需要在AndroidManifest.xml里进行下类设定
    <uses-permission android:name=”android.permission.VIBRATE”/>

    例程源码(Java)
    public class VibratorTestActivity extends Activity {
       
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);

            long[] pattern = {3000, 1000, 2000, 5000, 3000, 1000}; // OFF/ON/OFF/ON                

            vibrator.vibrate(pattern, -1);

        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
                vibrator.vibrate(10);
            }
            return super.onTouchEvent(event);

  • 相关阅读:
    jdbc preparedstatement 调用存储过程的问题
    httpclient 优化
    httpclient 4种关闭连接
    Cloudstack介绍(一)
    Docker registry私有仓库(七)
    Docker生产实践(六)
    python装饰器
    Docker镜像构建(五)
    python 生成器和迭代器介绍
    Docker数据管理(四)
  • 原文地址:https://www.cnblogs.com/wenfei123chai/p/4256467.html
Copyright © 2011-2022 走看看