Changing the Vibrate settings in Jelly Bean, Andro

2019-04-13 15:12发布

I am looking for how we change the vibrate settings in Jelly Bean. I found that all the pre-JB vibrate settings have been deprecated, but don't see any new AudioManager.[change the vibrate settings] code anywhere. There is a setting "Vibrate when ringing" which I would like to know how to play with.

Thanks for you help.

4条回答
可以哭但决不认输i
2楼-- · 2019-04-13 15:51

This issue has plagued me for a couple of days now. Finally got it working. Make sure that you have the right permissions as well.

uses-permission android:name="android.permission.WRITE_SETTINGS"/

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

}

Then in my on click listener I utilized the following:

                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();
                        }
                    }
                }
查看更多
Viruses.
3楼-- · 2019-04-13 15:57

From the Documentation:

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

Seems like Google wants Vibration settings to be handled by each app for itself on an app to app basis, adjusting it's settings by querying getRingerMode().

查看更多
叛逆
4楼-- · 2019-04-13 16:06

Settings.System.VIBRATE_WHEN_RINGING is declared with @hide annotation so you may have troubles to use it in Android Studio. So maybe you have to replace Settings.System.VIBRATE_WHEN_RINGING by its value : "vibrate_when_ringing"

It is annoying that it is declared with the @hide annotation, because it means that Google don't want external developpers using it...

查看更多
聊天终结者
5楼-- · 2019-04-13 16:16

In android4.1 you can use this to control "vibrate & ringing"

Settings.System.putInt(mContentResolver, Settings.System.VIBRATE_WHEN_RINGING, enable ? 1 : 0);
查看更多
登录 后发表回答