【每日一点】android 震动

来源:互联网 发布:管家婆软件那三个 编辑:程序博客网 时间:2024/05/19 13:27

安卓开发练习册
2015-10-24


  • 获得振动器实例
Vibrator vb = (Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
  • 判断该设备有无震动电机
  if(vb.hasVibrator()==true) tv_confirm.setText("该设备有震动器"); else tv_confirm.setText("该设备无震动器");
或者换一种写法:St_confirm = vb.hasVibrator() ? "该设备有震动器" : "该设备无震动器";tv_confirm.setText(St_confirm);
  • 开始震动按钮的点击监听器
    bt_StartMove.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {        if ("".equals(et_MoveSeconed.getText().toString()))         Toast.makeText(MainActivity.this, "please input ms", Toast.LENGTH_SHORT).show();        else {              movingseconed = Integer.parseInt(et_MoveSeconed.getText().toString().trim());         //vb.vibrate(movingseconed);        vb.vibrate(new long[] { 1000, movingseconed }, 0);// 0表示一直震动,-1表示只震动一次        startMoveFlag = true;                }            }        });

解释:
点击之后先查看et_MoveSeconed里有没有写东西,如果一开始没输入任何数字,那么getText().toString之后是空符号用“”表示。如果为空则做出漂浮TOAST提示“please input ms”,if里面作比较是请务必使用"".equals(StringXXX)空符号最好在前面。
笔记:由string转换成int,int xxx= Integer.parseInt(string xxx);
         vb.vibrate(movingseconed);的功能是指震动一次且movingseconed是震动的时间毫秒。
         vb.vibrate(new long[] { 1000, movingseconed }, 0);该方法第二个参数如果是 0表示一直震动,-1表示只震动一次;第一个参数是震动模式pattern,必须是long型数组,数组第一个参数代表震动前的等待时间毫秒,第二个数值表示震动器震动的时间毫秒。我这里让震动间隔为1000ms 震动的时间自定义读取输入EditView的值。
        最后把正在震动的标志位打开。
              

  • 关闭震动和关闭activity
@Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        if (keyCode == KeyEvent.KEYCODE_BACK) {            // 关闭震动            if (startMoveFlag) {                vb.cancel();                startMoveFlag = false;            } else {                finish();            }        }        return false;    }

在你的activity里定义一个onKeyDown 公共方法,
keyCode == KeyEvent.KEYCODE_BACK如上图。
如果按键点击之后执行了onclick里的代码让手机震动了,那肯定也将
startMoveFlag 打开了;所以如果startMoveFlag 状态是true,那么按返回键时我们执行将震动关掉的指令vb.cancel();并将startMoveFlag 关掉

否则在手机不震动的时候按返回键的话是退出activity。


  • 完整代码:
package com.example.zhenzhen;import android.R.bool;import android.app.Activity;import android.app.AlertDialog;import android.app.Service;import android.os.Bundle;import android.os.Vibrator;import android.view.KeyEvent;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {    private Vibrator vb = null;    private TextView tv_confirm = null;    private String St_confirm = null;    private EditText et_MoveSeconed = null;    private Button bt_StartMove = null;    private int movingseconed = 0;    private boolean startMoveFlag = false;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        IconDeclare();        vb = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);        // if(vb.hasVibrator()==true) tv_confirm.setText("该设备有震动器");        // else tv_confirm.setText("该设备无震动器");        St_confirm = vb.hasVibrator() ? "该设备有震动器" : "该设备无震动器";        tv_confirm.setText(St_confirm);        bt_StartMove.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                if ("".equals(et_MoveSeconed.getText().toString()))                    Toast.makeText(MainActivity.this, "please input ms", Toast.LENGTH_SHORT).show();                else {                    movingseconed = Integer.parseInt(et_MoveSeconed.getText().toString().trim());                    Toast.makeText(MainActivity.this, " " + movingseconed, Toast.LENGTH_SHORT).show();                    //vb.vibrate(movingseconed);                    vb.vibrate(new long[] { 1000, movingseconed }, 0);// 0表示一直震动,-1表示只震动一次                    startMoveFlag = true;                }            }        });    }    private void IconDeclare() {        tv_confirm = (TextView) findViewById(R.id.tv_confirm);        et_MoveSeconed = (EditText) findViewById(R.id.et_MoveSeconed);        bt_StartMove = (Button) findViewById(R.id.bt_StartMove);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }    @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        if (keyCode == KeyEvent.KEYCODE_BACK) {            // 关闭震动            if (startMoveFlag) {                vb.cancel();                startMoveFlag = false;            } else {                finish();            }        }        return false;    }}
0 0