私はそれを試してみた自分を。
private Vibrator vibrator;
private CheckBox checkbox;
private Thread vibrateThread;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vibrator = ((Vibrator)getSystemService(VIBRATOR_SERVICE));
checkbox = (CheckBox)findViewById(R.id.checkBox1);
vibrateThread = new VibrateThread();
}
public void onCheckBox1Click(View view) throws InterruptedException{
if(checkbox.isChecked()){
if (vibrateThread.isAlive()) {
vibrateThread.interrupt();
vibrateThread = new VibrateThread();
} else {
vibrateThread.start();
}
} else{
vibrateThread.interrupt();
vibrateThread = new VibrateThread();
}
}
class VibrateThread extends Thread {
public VibrateThread() {
super();
}
public void run() {
while(checkbox.isChecked()){
try {
vibrator.vibrate(1000);
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
そして、ここでレイアウト:
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox"
android:onClick="onCheckBox1Click"/>
私はそれが継続的にそれがCONTようTHTはそれを作るのThread.sleep置くカントー振動するようにしたい私は、以下のコードは、あなたが探しているものだと思います。振動する。 –
私はそれをテストしていませんが、睡眠の値が振動の値より低い限り、それはそのように動作するはずです。 –
私はしようとしましたが、それでもまだ動作しませんでした。今は2つのボタンを押して、キャンセルして1つ起動するようにしてもよいと思います。あなたが何かを考えているなら、共有してください。 –