【问题标题】:Changing the Vibrate settings in Jelly Bean, Android更改 Android Jelly Bean 中的振动设置
【发布时间】:2012-07-01 22:28:00
【问题描述】:

我正在寻找我们如何更改 Jelly Bean 中的振动设置。我发现所有 pre-JB 振动设置都已弃用,但在任何地方都看不到任何新的 AudioManager.[更改振动设置] 代码。有一个设置“振铃时振动”,我想知道如何玩。

感谢您的帮助。

【问题讨论】:

    标签: android android-4.2-jelly-bean vibration android-audiomanager


    【解决方案1】:

    在android4.1你可以用它来控制“振动和振铃”

    Settings.System.putInt(mContentResolver, Settings.System.VIBRATE_WHEN_RINGING, enable ? 1 : 0);
    

    【讨论】:

    • 非常感谢您!我已经在整个万维网上搜索了这个答案。谢谢!
    • 到目前为止,这就像一个魅力,在 Android M 上它会抛出一个 IllegalArgumentException:“你不能更改私人安全设置。”
    【解决方案2】:

    来自文档:

    This method is deprecated.
    Applications should maintain their own vibrate policy based on current ringer mode that can be queried via getRingerMode().
    

    似乎谷歌希望每个应用程序在应用程序到应用程序的基础上自行处理振动设置,通过查询 getRingerMode() 来调整其设置。

    【讨论】:

    • 但是 getRingerMode() 不会返回“振铃时振动”设置。
    【解决方案3】:

    Settings.System.VIBRATE_WHEN_RINGING 是用@hide 注解声明的,因此您在 Android Studio 中使用它可能会遇到麻烦。 所以也许你必须用它的值替换Settings.System.VIBRATE_WHEN_RINGING"vibrate_when_ringing"

    使用@hide 注释声明它很烦人,因为这意味着 Google 不希望外部开发人员使用它...

    【讨论】:

      【解决方案4】:

      这个问题已经困扰我好几天了。终于让它工作了。确保您也拥有正确的权限。

      使用权限 android:name="android.permission.WRITE_SETTINGS"/

      protected void onCreate(Bundle savedInstanceState) {
          audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
      
      }
      

      然后在我的点击监听器中,我使用了以下内容:

                      lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                      @Override
                      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                          if (vibrate == 1) {
                              try {
                                  Settings.System.putInt(getContentResolver(), "vibrate_when_ringing", 1);
                                  audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON); //Set it to never vibrate on ring:
                                  audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_ON); //Set it to never vibrate on notify
      
                                  boolean vibrateWhenRinging;
                                  vibrateWhenRinging = (Settings.System.getInt(getContentResolver(), "vibrate_when_ringing") == 1);
                                  Toast.makeText(MainActivity.this, Boolean.toString(vibrateWhenRinging), Toast.LENGTH_LONG).show();
                              } catch (Exception e) {
                                  Toast.makeText(MainActivity.this, "System vibrate error", Toast.LENGTH_LONG).show();
                              }
                          } else {
                              try {
                                  Settings.System.putInt(getContentResolver(), "vibrate_when_ringing", 0);
                                  audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF); //Set it to never vibrate on ring:
                                  audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF); //Set it to never vibrate on notify
      
                                  boolean vibrateWhenRinging;
                                  vibrateWhenRinging = (Settings.System.getInt(getContentResolver(), "vibrate_when_ringing") == 1);
                                  Toast.makeText(MainActivity.this, Boolean.toString(vibrateWhenRinging), Toast.LENGTH_LONG).show();
                              } catch (Exception e) {
                                  Toast.makeText(MainActivity.this, "System vibrate error in else statement", Toast.LENGTH_LONG).show();
                              }
                          }
                      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多