Android 在复选框上振动

Android vibrate on checkbox

我想构建一个简单的应用程序,它在单击复选框时振动并在再次单击后停止:

目前看起来像这样:

import android.content.Context;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Vibrator vibrator = (Vibrator) MainActivity.this.getSystemService(Context.VIBRATOR_SERVICE);
        final CheckBox vibrateCheckBox = (CheckBox) findViewById(R.id.checkPowerStrong);

        if(vibrateCheckBox.isChecked()) {
            while(vibrateCheckBox.isChecked()) {
                vibrator.vibrate(1000);
            }
        } else {
            vibrator.cancel();
        }

    }
}

但我收到一个错误:

Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()

我已授予清单振动权限:

如何解决这个问题

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

vibrateCheckBox = (CheckBox)findViewById(R.id.checkPowerStrong);
vibrateCheckBox.setChecked(false);
Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);



vibrateCheckBox .setOnCheckedChangeListener(new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked)
        {
             v.vibrate(500);     // Vibrate for 500 milliseconds
         }else
         {
            v.cancel();
         }
    }
});

}

Manifest.xml中添加权限

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

如何无限振动

// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// Start without a delay
// Vibrate for 100 milliseconds
// Sleep for 1000 milliseconds
long[] pattern = {0, 100, 1000};

// The '0' here means to repeat indefinitely
// '0' is actually the index at which the pattern keeps repeating from (the start)
// To repeat the pattern from any other point, you could increase the index, e.g. '1'
v.vibrate(pattern, 0);

当您准备停止振动时,只需调用cancel()方法:

v.cancel();

如何使用振动模式

If you want a more bespoke vibration, you can attempt to create your own vibration patterns:

// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// Start without a delay
// Each element then alternates between vibrate, sleep, vibrate, sleep...
long[] pattern = {0, 100, 1000, 300, 200, 100, 500, 200, 100};

// The '-1' here means to vibrate once, as '-1' is out of bounds in the pattern array
v.vibrate(pattern, -1);

它完全符合您的要求。做这样的事情

final Vibrator vibrator = (Vibrator) MainActivity.this.getSystemService(Context.VIBRATOR_SERVICE);
final CheckBox vibrateCheckBox = (CheckBox) findViewById(R.id.checkPowerStrong);

final Handler handler = new Handler();

final Runnable r = new Runnable() {
    public void run() {
        vibrator.vibrate(1000);
        handler.postDelayed(this, 1000);
    }
};

vibrateCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

       @Override
       public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
           if(vibrateCheckBox.isChecked()) {
               handler.postDelayed(r, 100);
           } else {
               handler.removeCallbacks(r);
               vibrator.cancel();
           }

       }
   }
);