我想构建一个简单的应用程序,点击复选框后会振动,然后在点击后停止:
目前它看起来像这样:
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();
}
}
}
但我确实收到了错误:
引起:java.lang.IllegalStateException:系统服务没有 在onCreate()
之前可用于活动
我给了清单振动的许可:
如何解决这个问题
答案 0 :(得分: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();
}
}
}
);
答案 1 :(得分:0)
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);