2

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

谢谢你的帮助。

4

4 回答 4

6

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

Settings.System.putInt(mContentResolver, Settings.System.VIBRATE_WHEN_RINGING, enable ? 1 : 0);
于 2012-07-30T07:33:36.143 回答
2

从文档:

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

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

于 2012-07-01T22:46:14.743 回答
0

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

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

于 2015-02-19T10:46:55.980 回答
0

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

使用权限 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();
                        }
                    }
                }
于 2015-07-14T14:16:41.880 回答