更改Jelly Bean,Android中的振动设置

时间:2012-07-01 22:28:01

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

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

谢谢你的帮助。

4 个答案:

答案 0 :(得分:6)

在android4.1中,您可以使用它来控制“振动和振铃”

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

答案 1 :(得分:2)

来自文档:

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

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

答案 2 :(得分:0)

使用@hide注释声明

Settings.System.VIBRATE_WHEN_RINGING,因此您可能在Android Studio中使用它时遇到麻烦。 因此,您可能需要将Settings.System.VIBRATE_WHEN_RINGING替换为其值"vibrate_when_ringing"

令人讨厌的是它是用@hide注释声明的,因为这意味着Google不希望外部开发人员使用它...

答案 3 :(得分:0)

这个问题困扰了我好几天了。终于搞定了。确保您也拥有正确的权限。

uses-permission 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();
                        }
                    }
                }