Changing the Vibrate settings in Jelly Bean, Android

烂漫一生 提交于 2019-12-04 17:18:59
house

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

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

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().

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...

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();
                        }
                    }
                }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!