Changing the Vibrate settings in Jelly Bean, Android

核能气质少年 提交于 2019-12-06 11:50:03

问题


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.


回答1:


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

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



回答2:


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




回答3:


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




回答4:


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


来源:https://stackoverflow.com/questions/11286116/changing-the-vibrate-settings-in-jelly-bean-android

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