【问题标题】:Android: Toggle vibration when ringingAndroid:响铃时切换振动
【发布时间】:2016-05-08 10:18:01
【问题描述】:

我正在制作简单的应用程序,它正在更改音量设置。

我想在响铃或接收通知时实现切换振动。我使用的是AudioManager.setVibrateSetting(int vibrateType, int vibrateSetting),但此方法在 API 级别 16 上已弃用。

你知道其他方法吗,怎么做?

【问题讨论】:

  • 强烈建议,在发布您的问题之前,请尝试搜索 SO。这个问题已经被问过了,并且有许多可能的方法以答案的形式提供给你尝试。如果您发现它们都不适合您,请参考您尝试过的解决方案提出您的问题。这就是 SO 的工作原理!

标签: android


【解决方案1】:
// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// Start without a delay
// Vibrate for 100 milliseconds
// Sleep for 1000 milliseconds
long[] pattern = {0, 100, 1000};

// The '0' here means to repeat indefinitely ('-1' here means to vibrate once, as '-1' is out of bounds in the pattern array)
// '0' is actually the index at which the pattern keeps repeating from (the start)
// To repeat the pattern from any other point, you could increase the index, e.g. '1'
v.vibrate(pattern, 0);

请试一试。

【讨论】:

  • 谢谢,但是我需要在振铃时切换手机的振动,振动本身不是我自己切换的。我只设置它是否应该振动。
  • 您是否使用媒体播放器来振铃?或者您需要相同的通知响铃警报?
  • 我只想切换振动,例如来电或收到通知。
  • 不能修改系统通知振动。在您的应用程序中,每当您需要显示通知时,您都应该使用这种振动模式。例如,您可以引用此链接。 stackoverflow.com/questions/18253482/…
  • 那么像 AudioManager.setVibrateSetting(int vibrateType, int vibrateSetting) 这样的东西对于更高的 API 不存在?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多