-->

vibrate_when_ringing on Android Marshmallow (6.0)

2019-04-11 12:13发布

问题:

In my code I have:

Settings.System.putInt(this.getContentResolver(), "vibrate_when_ringing", isVibrateWhenRinging ? 1 :0);

Using the following permission:

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

This was working fine from Jelly Bean (API 16) till Lollipop (API 22).

In Android M, I know that for using that permission I need to prompt the user to Settings.ACTION_MANAGE_WRITE_SETTINGS.

However, even with that permission turned on, I see the following error:

E/AndroidRuntime: java.lang.IllegalArgumentException: You cannot change private secure settings.
E/AndroidRuntime:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:165)
E/AndroidRuntime:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
E/AndroidRuntime:     at android.content.ContentProviderProxy.call(ContentProviderNative.java:646)

Well... Am I missing something? With proper permissions we can change ringtones, do not disturb mode, etc. But it also looks like that with Android M we won't be able to change such a normal setting like "Vibrate when ringing". I hope I'm wrong.

回答1:

You should call Settings.System.canWrite() to see that you can to write this setting.

If function returns false then the user can agree to allow your app to write to settings:

Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + getApplicationContext().getPackageName()));
startActivity(intent);


回答2:

I am going to answer my own question. There is an issue opened to Google: https://code.google.com/p/android/issues/detail?id=194376.

Basically, they partially fixed vibrate_when_ringing and it works again on Android N (24). Unfortunately, it does not work on Android M (23). For the response on the issue, it does not seem that is going to be fixed.

You can see a full example repo here: https://github.com/ferrannp/VibrateWhenRinging

So, basically, your code needs to check if your on Android M and if you are, do not use that setting.