我正在尝试在我的自定义Android应用程序中静音警报。对于Nuggets(Android N),我使用以下代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (notificationManager != null && !notificationManager.isNotificationPolicyAccessGranted()) {
try {
startActivityForResult(new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS), 0);
} catch (Exception e) {
e.printStackTrace();
}
}
}
它的工作正常。并且使整个声音静音甚至报警。但它没有在Marshmallow.it上工作,这给了我一个例外,即startActivityforResult()之后每次都找不到这个类。
Unable to instantiate activity ComponentInfo{com.android.settings/com.android.settings.Settings$ZenAccessSettingsActivity}: java.lang.ClassNotFoundException: Didn't find class "com.android.settings.Settings$ZenAccessSettingsActivity"
现在我正在使用下面的代码来消除Marshmallow中的所有声音,但是报警仍然按时完成声音。有人告诉我我的代码中的错误在哪里?或者我使用错误的方法来做到这一点?
AudioManager mAlramMAnager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
if (mAlramMAnager != null) {
mAlramMAnager.adjustStreamVolume(AudioManager.STREAM_NOTIFICATION, AudioManager.ADJUST_MUTE, 0);
mAlramMAnager.adjustStreamVolume(AudioManager.STREAM_ALARM, AudioManager.ADJUST_MUTE, 0);
mAlramMAnager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_MUTE, 0);
mAlramMAnager.adjustStreamVolume(AudioManager.STREAM_RING, AudioManager.ADJUST_MUTE, 0);
mAlramMAnager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_MUTE, 0);
}