下面列出了android.os.VibrationEffect#createWaveform ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void startRinging() {
log("startRinging");
int ringerMode = ((AudioManager) getSystemService(AUDIO_SERVICE)).getRingerMode();
if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
return;
}
vibrator = ((Vibrator) getSystemService(VIBRATOR_SERVICE));
long[] pattern = {1500, 800, 800, 800};
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
VibrationEffect vibe = VibrationEffect.createWaveform(pattern, 0);
vibrator.vibrate(vibe);
} else {
vibrator.vibrate(pattern, 0);
}
if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) {
return;
}
ringtone = RingtoneManager.getRingtone(this, RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE));
ringtone.play();
}
private void ringPhone(){
int ringerMode = ((AudioManager) getSystemService(AUDIO_SERVICE)).getRingerMode();
if(ringerMode == AudioManager.RINGER_MODE_SILENT) return;
vibrator = ((Vibrator) getSystemService(VIBRATOR_SERVICE));
long[] pattern = {1500, 800, 800, 800};
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
VibrationEffect vibe = VibrationEffect.createWaveform(pattern, 0);
vibrator.vibrate(vibe);
}else{
vibrator.vibrate(pattern, 0);
}
if(ringerMode == AudioManager.RINGER_MODE_VIBRATE) return;
ringtone = RingtoneManager.getRingtone(this, RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE));
ringtone.play();
}
private static VibrationEffect createEffectFromTimings(long[] timings) {
if (timings == null || timings.length == 0) {
return null;
} else if (timings.length == 1) {
return VibrationEffect.createOneShot(timings[0], VibrationEffect.DEFAULT_AMPLITUDE);
} else {
return VibrationEffect.createWaveform(timings, -1);
}
}
public static void vibrate(Vibrator vibrator) {
long[] timings = {0, 1000, 1000};
int[] amplitudes = {0, VibrationEffect.DEFAULT_AMPLITUDE, 0};
VibrationEffect effect = VibrationEffect.createWaveform(timings, amplitudes, 1);
AudioAttributes audioAttrs =
new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
.build();
vibrator.vibrate(effect, audioAttrs);
}
VibratorService(Context context) {
vibratorInit();
// Reset the hardware to a default state, in case this is a runtime
// restart instead of a fresh boot.
vibratorOff();
mSupportsAmplitudeControl = vibratorSupportsAmplitudeControl();
mContext = context;
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*vibrator*");
mWakeLock.setReferenceCounted(true);
mAppOps = mContext.getSystemService(AppOpsManager.class);
mBatteryStatsService = IBatteryStats.Stub.asInterface(ServiceManager.getService(
BatteryStats.SERVICE_NAME));
mPreviousVibrationsLimit = mContext.getResources().getInteger(
com.android.internal.R.integer.config_previousVibrationsDumpLimit);
mDefaultVibrationAmplitude = mContext.getResources().getInteger(
com.android.internal.R.integer.config_defaultVibrationAmplitude);
mAllowPriorityVibrationsInLowPowerMode = mContext.getResources().getBoolean(
com.android.internal.R.bool.config_allowPriorityVibrationsInLowPowerMode);
mPreviousVibrations = new LinkedList<>();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
context.registerReceiver(mIntentReceiver, filter);
VibrationEffect clickEffect = createEffectFromResource(
com.android.internal.R.array.config_virtualKeyVibePattern);
VibrationEffect doubleClickEffect = VibrationEffect.createWaveform(
DOUBLE_CLICK_EFFECT_FALLBACK_TIMINGS, -1 /*repeatIndex*/);
VibrationEffect heavyClickEffect = createEffectFromResource(
com.android.internal.R.array.config_longPressVibePattern);
VibrationEffect tickEffect = createEffectFromResource(
com.android.internal.R.array.config_clockTickVibePattern);
mFallbackEffects = new SparseArray<>();
mFallbackEffects.put(VibrationEffect.EFFECT_CLICK, clickEffect);
mFallbackEffects.put(VibrationEffect.EFFECT_DOUBLE_CLICK, doubleClickEffect);
mFallbackEffects.put(VibrationEffect.EFFECT_TICK, tickEffect);
mFallbackEffects.put(VibrationEffect.EFFECT_HEAVY_CLICK, heavyClickEffect);
mScaleLevels = new SparseArray<>();
mScaleLevels.put(SCALE_VERY_LOW,
new ScaleLevel(SCALE_VERY_LOW_GAMMA, SCALE_VERY_LOW_MAX_AMPLITUDE));
mScaleLevels.put(SCALE_LOW, new ScaleLevel(SCALE_LOW_GAMMA, SCALE_LOW_MAX_AMPLITUDE));
mScaleLevels.put(SCALE_NONE, new ScaleLevel(SCALE_NONE_GAMMA));
mScaleLevels.put(SCALE_HIGH, new ScaleLevel(SCALE_HIGH_GAMMA));
mScaleLevels.put(SCALE_VERY_HIGH, new ScaleLevel(SCALE_VERY_HIGH_GAMMA));
}