문제

I want to pause or resume my music when shaking the phone.

The problem is, while walking around with the phone in my pocket, the phone shake event is called by accident sometimes.

So, I want to combine the volume or on/off button with shaking the phone. How can I find out, whether the volume button is pressed at the moment.

I tried this, but this is only working when my app is in the forground. Is there a possiblity just to get the state, whether it is pressed at the moment or not, without a onkeydown listener?

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
        //Do something
    }
    return true;
}
도움이 되었습니까?

해결책

Well @sativa i think you can do it by using device sensor property which normally uses at time of calling, you can check if the device is in pocket or not, just like when we make call screen is gone to be lock if we keep our device closer to our ears. Think in this direction may be it will help you

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