문제

I declare my variable 'shake' as a global variable, then I created new object inside the oncreate, then I call this :

@Override
public void onPause() {
    super.onPause();
    shake.cancel();
}

my phone can still vibrate although home button is pressed! I tried onStop(), same doesn't work..

my app is like this : countdown 10 sec, after that vibrate.. but the problem is onPause cannot be call so the user may feel where's the vibrate come from if it's set 2 minutes on the countdown ticker.. help!

도움이 되었습니까?

해결책

Since I can't see the rest of your code, I'm gonna assume a few things.

Assumption #1

If you have your activity open, and the countdown starts and expires after 10 seconds, your phone vibrates (with your activity still open). If you go to home screen, the vibration stops.

Assumption #2

You have your activity open, and the countdown starts. Before the 10 second expires, you go to home screen. Your activity is not visible, but the phone starts vibrating soon.

If this is what you are seeing, it's the correct behavior. The problem is that in the 2nd case, your shake.cancel() from onPause() is called when you go to the home screen, before it actually starts vibrating. shake.cancel() can only cancel if it's already vibrating.

If that's what you are trying to fix (I can only assume since I can't see the rest of your code), you can try this:

private boolean mAllowShake = false;
@Override
public void onResume() {
   super.onResume();
   mAllowShake = true;
}

@Override
pulic void onPause() {
   super.onPause();
   mAllowShake = false;
   shake.cancel();
}

// wherever you are calling the shake.vibrate()
if (mAllowShake)
   shake.vibrate();

This way, when your activity is not visible and your timer goes off, since mAllowShake is false, it won't actually vibrate.

If that's not what you are trying to fix, please update your question with more code and description of your exact use case. Hope it helps!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top