我在app(在Studio中)制作计时器,当计时器达到0时,它会振动。我想这样做,以便在屏幕锁定时手机仍然振动。有什么建议吗?
这是我的主要活动:
EditText timerWrite;
ImageButton power;
ImageButton check;
TextView minsLeft;
CountDownTimer countDownTimer = null;
Vibrator vibe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen);
vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
timerWrite = (EditText) findViewById(R.id.timerWrite);
power = (ImageButton) findViewById(R.id.power);
check = (ImageButton) findViewById(R.id.check);
minsLeft = (TextView) findViewById(R.id.minsLeft);
power.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
vibe.cancel();
if(countDownTimer != null)
countDownTimer.cancel();
minsLeft.setText("stopped");
}
});
check.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
vibe.cancel();
if (countDownTimer != null)
countDownTimer.cancel();
String text = timerWrite.getText().toString();
if (!text.equalsIgnoreCase("")) {
int mins = Integer.valueOf(text);
countDownTimer = new CountDownTimer(mins * 60000, 1000) {
@Override
public void onTick(long milliseconds) {
long minutes = milliseconds / 60000;
long seconds = (milliseconds / 1000) % 60;
minsLeft.setText(getString(R.string.minutes) + (int) (minutes) + " seconds:" + (int) (seconds));
}
@Override
public void onFinish() {
minsLeft.setText("Are You OK?");
long[] pattern = {0, 1000, 2000, 100};
vibe.vibrate(pattern, 1);
}
}.start();
}
}
});
}
谢谢!
答案 0 :(得分:0)
这将超出活动生命周期,因为它将转到onPause()
和onStop()
,因此您的代码/应用程序将无法正常工作,您正在寻找的服务将在没有这些限制的情况下运行。
https://developer.android.com/guide/components/services.html
干杯。