Vibrate setting not turning off on receiving incom

2019-04-06 18:22发布

问题:

I want to turn off device vibrate setting when a call comes. I have implemented a BroadcastReceiver for this feature which performs the action on receiving PHONE_STATE broadcast. The problem is that I am not able to turn off vibrations at all. I have tried the following:

AudioManager audioManager = (AudioManager)
                            context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, 
                               AudioManager.VIBRATE_SETTING_OFF);

or

Vibrator vib = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vib.cancel();

The first approach seems ideal to me. It even works for turning "on" the vibrations when a call is received. But, I am not able turn them "off" in this scenario.

Has anybody tried this?

Edit: I checked Android's Phone app code. Following code is present in Ringer.java:

if (shouldVibrate() && mVibratorThread == null) {
    mContinueVibrating = true;
    mVibratorThread = new VibratorThread();
    mVibratorThread.start();
}

A thread is started initially by Phone app which vibrates the phone. When I change the vibrate setting to off this check is skipped but the already started thread keeps on running.

This also explains how vibrations can be turned on when an incoming call comes. In that case, the thread is not running initially. Then, it is started when I turn on the vibrate setting. I don't think there is any solution to the problem without changing the Phone app.

回答1:

this:

Vibrator vib = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vib.cancel();

is not going to help you as it cancels the new instance of the Vibrator.

I can imagine that it's too late to turn of the vibration of the device if the call is already incoming as the vibration is already in progress. So you'd have to do that before the call comes in.