Simulating vibration as when receiving a call
So I am trying to simulate that the phone is receiving a call. I have successfully extracted the phones ring tone and played it. Now I want to simulate the vibration. While I can make the phone vibrate, I want to emulate the exact pattern that the phone vibrates with as when it receives a call. Is there开发者_如何学C some setting or class that I can use to extract this pattern, and also detect if vibration is turned on?
You have to vibrate it in a pattern.
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// 1. Vibrate for 1000 milliseconds
long milliseconds = 1000;
v.vibrate(milliseconds);
// 2. Vibrate in a Pattern with 500ms on, 500ms off for 5 times
long[] pattern = { 500, 300 };
v.vibrate(pattern, 5);
http://www.androidsnippets.org/snippets/22/
I'm not sure what pattern is used as standard, you could probably find it in the source, or else keep trying different patterns yourself until it is satisfactory.
It seems like there is no class that can provide you with 'standart' vibration settings for incoming call. Since most phones come with custom vendor caller apps and since there are a lot of custom caller apps on Google Play, this 'standart' pattern probably doesn't even exist.
Standart caller app from AOSP uses this pattern:
private static final int VIBRATE_LENGTH = 1000; // ms
private static final int PAUSE_LENGTH = 1000; // ms
And to detect if the vibration is turned on:
boolean shouldVibrate() {
AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
int ringerMode = audioManager.getRingerMode();
if (Settings.System.getInt(mContext.getContentResolver(), "vibrate_when_ringing", 0) > 0) {
return ringerMode != AudioManager.RINGER_MODE_SILENT;
} else {
return ringerMode == AudioManager.RINGER_MODE_VIBRATE;
}
}
This relies on the "Vibrate while ringing" and "Sound mode" settings you can find in standart phone settings.
Why not use the Android source in order to see how they do it?
The Phone app source is available from
https://android.googlesource.com/platform/packages/apps/Phone
精彩评论