问题
Somehow tricky question. I am working with one app through which user can set incoming call custom ringtone and different vibration level for different contacts.
I have stuck with vibration level setting. We can set vibration level using,
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);
This is what about vibrate my phone. But I want to set vibration level of incoming call. User can set from different predefined vibration settings.
Using this code I can set Vibration ON - OFF. But dont know how to set level of vibration.
String VIBRATE_IN_SILENT_SETTING_NAME = "vibrate_in_silent";
Settings.System.putInt(getContentResolver(), VIBRATE_IN_SILENT_SETTING_NAME, 1);
I hope that someone could give some advice on this issue. suggestions are welcomed.
回答1:
The Android Vibrate API has only time control. There is no magnitude control in that API.
For more refer this
http://developer.android.com/reference/android/os/Vibrator.html
回答2:
If you want to make lower vibration you need to create a pattern with blankspaces in the middle.
Use constants like:
int dot = 200;
int short_gap = 200; // Length of Gap
and then define array and pattern:
long[] pattern = {
0, // Start immediately
dot, short_gap };
then you can call:
v.vibrate(pattern, x);
// x value:
// 0 repeat infinite
// -1 no repeat
// n number of repetitions
By changing lenght of the dot and gap (or defining new ones to perform various patterns) you can modulate power of vibration.
EDIT1: Here is my VibrationConstants.java
public class VibrationConstants {
private static int p = 5; // five millisecond of vibration
private static int s = 5; // five millisecond of break - could be more to
// weaken
// the vibration
private static int pp = 10; // ten millisecond of vibration
private static int ss = 10; // ten millisecond of break - could be more to
// weaken
// the vibration
private static int ppp = 50; // fifty millisecond of vibration
private static int sss = 50; // fifty millisecond of break - could be more to
// weaken
// the vibration
public static long[] HARD_VIBRATION = { 0, ppp, sss };
public static long[] MIDDLE_VIBRATION = { 0, pp, ss };
public static long[] SOFT_VIBRATION_2 = { 0, p, s };
public static long[] PATTERN_VIBRATION = { 0, 250, 200, 250, 150, 150, 75,
150, 75, 150 };
}
So, when you want to call it:
v.vibrate(VibrationConstants.SOFT_VIBRATION_2, x);
// x value:
// 0 repeat infinite
// -1 no repeat
// n number of repetitions
Try also by changing ONLY the break values if the result with same values in vibration and breaks is not accurated as you expect. This make nice effects. :)
Hope that helps, i tried to found patterns from phone brands like Google Samsung or Xperia, but i couldnt find it... so we had nice results with that patterns to simulate, but all depends of your requirements.
Im sure you will have to make some tests to reach ur target, but is not so hard.
来源:https://stackoverflow.com/questions/26804939/how-to-change-incoming-call-vibration-level-when-incoming-call-made