4.1的震子变成一个服务,调用方式有所变化:
private static final int VIBRATE_DURATION = 15; private Vibrator mVibrator; mVibrator = (Vibrator) launcher.getSystemService(Context.VIBRATOR_SERVICE); mVibrator.vibrate(VIBRATE_DURATION); //以前的用法如下: private Vibrator mVibrator = new Vibrator(); mVibrator.vibrate(VIBRATE_DURATION); //END
一般也可以利用view的HapticFeedback来实现触摸点击反馈:
v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
该参数在设置的声音选项触摸时振动设置并存储为系统数据,view在刷新绘制时会获取该值然后进行振动回馈。
有时候需要定制一个view的振动强度这时候我们可以两者搭配使用:
public void performVibrate(int adjustment){ int val = new Settings.System().getInt(getContentResolver(),Settings.System.HAPTIC_FEEDBACK_ ENABLED,0); if (DEBUG_WIDGETS) { Log.d(TAG,"-----performVibrate for onLongClick----" + val); } if (val != 0){ mVibrator.vibrate(VIBRATE_DURATION + adjustment); } }定制化view的振动时需要关闭它自身的HapticFeedback,不然会出现振动时间和设值不一致的情况:
launcher.setupViews:mWorkspace.setHapticFeedbackEnabled(false); AppsCustomizePagedView.syncAppsPageItems:icon.setHapticFeedbackEnabled(false);