android 入门学习笔记 手机震动的节奏---vibrate对象及周期运用 和ToggleButton

 需要引入import android.os.Vibrator;/*设置ToggleButton的对象*/ mVibrator01 = ( Vibrator )getApplication().getSystemService (Service.VIBRATOR_SERVICE); final ToggleButton mtogglebutton1 = (ToggleButton) findViewById(R.id.myTogglebutton1); final ToggleButton mtogglebutton2 = (ToggleButton) findViewById(R.id.myTogglebutton2); final ToggleButton mtogglebutton3 = (ToggleButton) findViewById(R.id.myTogglebutton3); /* 短震动 */ mtogglebutton1.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (mtogglebutton1.isChecked()) { /* 设置震动的周期 */ mVibrator01.vibrate( new long[]{100,10,100,1000},-1); /*用Toast显示震动启动*/ Toast.makeText ( EX05_06.this, getString(R.string.str_ok), Toast.LENGTH_SHORT ).show(); } else { /* 取消震动 */ mVibrator01.cancel(); /*用Toast显示震动已被取消*/ Toast.makeText ( EX05_06.this, getString(R.string.str_end), Toast.LENGTH_SHORT ).show(); } } }); /* 长震动 */ mtogglebutton2.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (mtogglebutton2.isChecked()) { /*设置震动的周期*/ mVibrator01.vibrate(new long[]{100,100,100,1000},0); /*用Toast显示震动启动*/ Toast.makeText ( EX05_06.this, getString(R.string.str_ok), Toast.LENGTH_SHORT ).show(); } else { /* 取消震动 */ mVibrator01.cancel(); /* 用Toast显示震动取消 */ Toast.makeText ( EX05_06.this, getString(R.string.str_end), Toast.LENGTH_SHORT ).show(); } } }); /* 节奏震动 */ mtogglebutton3.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (mtogglebutton3.isChecked()) { /* 设置震动的周期 */ mVibrator01.vibrate( new long[]{1000,50,1000,50,1000},0); /*用Toast显示震动启动*/ Toast.makeText ( EX05_06.this, getString(R.string.str_ok), Toast.LENGTH_SHORT ).show(); } else { /* 取消震动 */ mVibrator01.cancel(); /* 用Toast显示震动取消 */ Toast.makeText ( EX05_06.this, getString(R.string.str_end), Toast.LENGTH_SHORT ).show(); } } });


震动事件必须允许android.permission.VIBRATE权限

<uses-permission android:name="android.permission.VIBRATE" />


 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/1aaeb32902e1ac16dd1e69f5a6854619.html