How to provide customized vibration on specific in

2019-03-14 11:23发布

问题:

The program functions like this: the user has a list of phone numbers, for which the cellphone could vibrate upon an incoming call only when no other system-wide application would provide vibration (such as in mute mode). I know that this is somehow against the rules, for that an application should respect the users' settings, but the application is limited to some certain users with this need. I have tried two ways but neither of them are satisfying:

  1. Listen to the telephony state and directly trigger the vibration service with my own pattern (with Vibrator.vibrate()). This method is effective with no incoming calls yet randomly effective when the phone is in CALL_STATE_RINGING state and I guess it's because of the conflict with the system-wide application that actually handles the vibration upon incoming call.

  2. Judge whether the cellphone is vibrating upon an incoming call (with AudioManager.shouldVibrate()), and decide whether to change the vibrate settings (with AudioManager.setRingerMode() and AudioManager.setVibrateSetting()). If the vibrate settings are changed by my application, they are to be restored once the cellphone is back to CALL_STATE_IDLE state. This method, however, is still not functioning sometimes, without any sign of the reason.

I hope that someone could give some advice on this issue. Comments on these two ways or other suggest are welcome.

回答1:

You need to play with two settings in order for your phone to vibrate. The first one is the sound mode which needs to be set by using AufioManager:

AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(targetSoundMode);

The second part is not properly documented and I believe this is the missing part of your code:

String VIBRATE_IN_SILENT_SETTING_NAME = "vibrate_in_silent";
Settings.System.putInt(getContentResolver(), VIBRATE_IN_SILENT_SETTING_NAME, 1);

use 1 to turn vibrate on and 0 to turn vibrate off.

to fully understand how you should work with vibrate settings and mode take a look at the following link: http://hi-android.info/src/com/android/settings/SoundSettings.java.html