下面列出了android.os.Vibrator#hasVibrator ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private boolean shouldVibrateNew(Context context, int ringerMode) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
if (vibrator == null || !vibrator.hasVibrator()) {
return false;
}
boolean vibrateWhenRinging = Settings.System.getInt(context.getContentResolver(), "vibrate_when_ringing", 0) != 0;
if (vibrateWhenRinging) {
return ringerMode != AudioManager.RINGER_MODE_SILENT;
} else {
return ringerMode == AudioManager.RINGER_MODE_VIBRATE;
}
}
/** Ensure that the vibration setting does not appear on devices without a vibrator. */
private void checkVibrationSupport() {
Activity activity = getActivity();
if (activity == null) {
return;
}
final Vibrator vibrator = (Vibrator) activity.getSystemService(VIBRATOR_SERVICE);
if (vibrator != null && vibrator.hasVibrator()) {
return;
}
final PreferenceGroup category =
(PreferenceGroup) findPreferenceByResId(R.string.pref_category_feedback_key);
final TwoStatePreference prefVibration =
(TwoStatePreference) findPreferenceByResId(R.string.pref_vibration_key);
if (prefVibration != null) {
prefVibration.setChecked(false);
category.removePreference(prefVibration);
}
}
private void vibrate()
{
Vibrator vb = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
if (vb != null && vb.hasVibrator())
{
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
VibrationEffect vibe = VibrationEffect.createOneShot(200, DEFAULT_AMPLITUDE);
vb.vibrate(vibe);
}
else
{
//noinspection deprecation
vb.vibrate(200);
}
}
}
@Override
public void onLongTokenClick(View view, Token token, List<BigInteger> tokenIds) {
//show radio buttons of all token groups
if (adapter != null) adapter.setRadioButtons(true);
selection = tokenIds;
Vibrator vb = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
if (vb != null && vb.hasVibrator()) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
VibrationEffect vibe = VibrationEffect.createOneShot(200, DEFAULT_AMPLITUDE);
vb.vibrate(vibe);
} else {
//noinspection deprecation
vb.vibrate(200);
}
}
//Wait for availability to complete
waitForMapBuild();
populateButtons(token, getSelectedTokenId(tokenIds));
showButtons();
}
@Override
public boolean onRequest(final Intent request, final Intent response) {
Vibrator vibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
if (vibrator == null || !vibrator.hasVibrator()) {
setResult(response, IntentDConnectMessage.RESULT_ERROR);
} else {
vibrator.cancel();
}
// cancel()は現在されているの振調パターンの1節しかキャンセルしないので、
// それ以降の振動パターンの節の再生を防ぐ為に、キャンセルされたことを示す
// フラグをたてる。
mIsCancelled = true;
setResult(response, IntentDConnectMessage.RESULT_OK);
return true;
}
/** Vibrates the phone. */
private void vibrate() {
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if (hasPermissions(this, Manifest.permission.VIBRATE) && vibrator.hasVibrator()) {
vibrator.vibrate(VIBRATION_STRENGTH);
}
}
public static void handleVibration(Context context) {
if(context == null) return;
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
if (!vibrator.hasVibrator()) return;
//Check if we should vibrate
boolean vibrateOnPress = PreferenceManager
.getDefaultSharedPreferences(context)
.getBoolean(Settings.KEY_PREF_VIBRATE_REMOTE_BUTTONS,
Settings.DEFAULT_PREF_VIBRATE_REMOTE_BUTTONS);
if (vibrateOnPress) {
vibrator.vibrate(UIUtils.buttonVibrationDuration);
}
}
/**
* @param context everything needs a context :(
*/
public LegacyGlobalActions(Context context, WindowManagerFuncs windowManagerFuncs,
Runnable onDismiss) {
mContext = context;
mWindowManagerFuncs = windowManagerFuncs;
mOnDismiss = onDismiss;
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
mDreamManager = IDreamManager.Stub.asInterface(
ServiceManager.getService(DreamService.DREAM_SERVICE));
// receive broadcasts
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
context.registerReceiver(mBroadcastReceiver, filter);
ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
// get notified of phone state changes
TelephonyManager telephonyManager =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
mContext.getContentResolver().registerContentObserver(
Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
mAirplaneModeObserver);
Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
mHasVibrator = vibrator != null && vibrator.hasVibrator();
mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
com.android.internal.R.bool.config_useFixedVolume);
mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
}
/**
* Get Vibrator if available in system.
*
* @param context to use for get
* @return {@link Vibrator} instance or null if not available in system.
*/
public static Vibrator getVibrator(Context context) {
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
if (v.hasVibrator())
return v;
return null;
}
public void startCallNotification() {
ringtonePlayer.play(false);
vibrator = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
long[] vibrationCycle = {0, 1000, 1000};
if (vibrator.hasVibrator()) {
vibrator.vibrate(vibrationCycle, 1);
}
}
/**
* Run the vibrator to give tactile feedback for 350ms when user authentication is successful.
*/
private void giveTactileFeedbackForAuthFail() {
if (!mIsTactileFeedbackEnabled) return;
final Vibrator v = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
if (v == null) {
Log.w("PasscodeView", "Vibrator service not found.");
return;
}
if (v.hasVibrator()) v.vibrate(350);
}
/**
* Run the vibrator to give tactile feedback for 100ms at difference of 50ms for two times when
* user authentication is failed.
*/
private void giveTactileFeedbackForAuthSuccess() {
if (!mIsTactileFeedbackEnabled) return;
final Vibrator v = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
if (v == null) {
Log.w("PasscodeView", "Vibrator service not found.");
return;
}
if (v.hasVibrator()) v.vibrate(new long[]{50, 100, 50, 100}, -1);
}
/**
* Run the vibrator to give tactile feedback for 50ms when any key is pressed.
*/
protected void giveTactileFeedbackForKeyPress() {
if (!mIsTactileFeedbackEnabled) return;
final Vibrator v = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
if (v == null) {
Log.w("PasscodeView", "Vibrator service not found.");
return;
}
if (v.hasVibrator()) v.vibrate(50);
}
public static boolean hasVibrator(Context con) {
if (mHasVibrator != null) return mHasVibrator;
try {
Vibrator v = (Vibrator) con.getSystemService(Context.VIBRATOR_SERVICE);
mHasVibrator = v.hasVibrator();
return mHasVibrator;
} catch (Throwable t) {
mHasVibrator = null;
return false;
}
}
private void stopVibrate(Context context, Intent intent) {
final Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
if (!vibrator.hasVibrator()) {
Toast.makeText(context, R.string.vibrator_not_found, Toast.LENGTH_LONG).show();
return;
}
vibrator.cancel();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(ToggleVibratePhone.NOTIFICATION_ID);
vibrating=false;
}
@Override
public void onConnectionStateChanged(int newState) {
if (newState == STATE_FAILED) {
callFailed();
return;
}
if (newState == STATE_ESTABLISHED) {
if (spPlayID != 0) {
soundPool.stop(spPlayID);
spPlayID = 0;
}
if(!wasEstablished){
wasEstablished=true;
if(!isProximityNear){
Vibrator vibrator=(Vibrator) getSystemService(VIBRATOR_SERVICE);
if(vibrator.hasVibrator())
vibrator.vibrate(100);
}
AndroidUtilities.runOnUIThread(new Runnable(){
@Override
public void run(){
if(controller==null)
return;
int netType=getStatsNetworkType();
StatsController.getInstance(currentAccount).incrementTotalCallsTime(netType, 5);
AndroidUtilities.runOnUIThread(this, 5000);
}
}, 5000);
if(isOutgoing)
StatsController.getInstance(currentAccount).incrementSentItemsCount(getStatsNetworkType(), StatsController.TYPE_CALLS, 1);
else
StatsController.getInstance(currentAccount).incrementReceivedItemsCount(getStatsNetworkType(), StatsController.TYPE_CALLS, 1);
}
}
if(newState==STATE_RECONNECTING){
if(spPlayID!=0)
soundPool.stop(spPlayID);
spPlayID=soundPool.play(spConnectingId, 1, 1, 0, -1, 1);
}
dispatchStateChanged(newState);
}
@Override
public void onConnectionStateChanged(int newState) {
if (newState == STATE_FAILED) {
callFailed();
return;
}
if (newState == STATE_ESTABLISHED) {
if(connectingSoundRunnable!=null){
AndroidUtilities.cancelRunOnUIThread(connectingSoundRunnable);
connectingSoundRunnable=null;
}
if (spPlayID != 0) {
soundPool.stop(spPlayID);
spPlayID = 0;
}
if(!wasEstablished){
wasEstablished=true;
if(!isProximityNear){
Vibrator vibrator=(Vibrator) getSystemService(VIBRATOR_SERVICE);
if(vibrator.hasVibrator())
vibrator.vibrate(100);
}
AndroidUtilities.runOnUIThread(new Runnable(){
@Override
public void run(){
if (tgVoip != null) {
StatsController.getInstance(currentAccount).incrementTotalCallsTime(getStatsNetworkType(), 5);
AndroidUtilities.runOnUIThread(this, 5000);
}
}
}, 5000);
if(isOutgoing)
StatsController.getInstance(currentAccount).incrementSentItemsCount(getStatsNetworkType(), StatsController.TYPE_CALLS, 1);
else
StatsController.getInstance(currentAccount).incrementReceivedItemsCount(getStatsNetworkType(), StatsController.TYPE_CALLS, 1);
}
}
if(newState==STATE_RECONNECTING){
if(spPlayID!=0)
soundPool.stop(spPlayID);
spPlayID=soundPool.play(spConnectingId, 1, 1, 0, -1, 1);
}
dispatchStateChanged(newState);
}
@Override
public void onConnectionStateChanged(int newState) {
if (newState == STATE_FAILED) {
callFailed();
return;
}
if (newState == STATE_ESTABLISHED) {
if(connectingSoundRunnable!=null){
AndroidUtilities.cancelRunOnUIThread(connectingSoundRunnable);
connectingSoundRunnable=null;
}
if (spPlayID != 0) {
soundPool.stop(spPlayID);
spPlayID = 0;
}
if(!wasEstablished){
wasEstablished=true;
if(!isProximityNear){
Vibrator vibrator=(Vibrator) getSystemService(VIBRATOR_SERVICE);
if(vibrator.hasVibrator())
vibrator.vibrate(100);
}
AndroidUtilities.runOnUIThread(new Runnable(){
@Override
public void run(){
if (tgVoip != null) {
StatsController.getInstance(currentAccount).incrementTotalCallsTime(getStatsNetworkType(), 5);
AndroidUtilities.runOnUIThread(this, 5000);
}
}
}, 5000);
if(isOutgoing)
StatsController.getInstance(currentAccount).incrementSentItemsCount(getStatsNetworkType(), StatsController.TYPE_CALLS, 1);
else
StatsController.getInstance(currentAccount).incrementReceivedItemsCount(getStatsNetworkType(), StatsController.TYPE_CALLS, 1);
}
}
if(newState==STATE_RECONNECTING){
if(spPlayID!=0)
soundPool.stop(spPlayID);
spPlayID=soundPool.play(spConnectingId, 1, 1, 0, -1, 1);
}
dispatchStateChanged(newState);
}
private void vibrate() {
if (mDontVibrate) return;
Vibrator vi = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if (!vi.hasVibrator()) return;
vi.vibrate(VIBRATION);
}
private void startVibrate() {
mVibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
if(mVibrator.hasVibrator()){
mVibrator.vibrate(new long[]{500, 1500, 500, 1500}, 0);//off on off on
}
}