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/8494.html