Hi I'm trying to add a mute button to my android project app just learning the basics here's what I did. I created a Button (this is just for now in the future I will try to use Switch) id set as android:id="@+id/muteBtn"
and declared the varaibles in java Button muteBtn = (Button) findViewById(R.id.muteBtn) ;
and when its click it must put the mode to silent mode I added a user-permission
because its giving me an error Not allowed to change Do Not Disturb state
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
and here's the button click listener
muteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& !notificationManager.isNotificationPolicyAccessGranted()) {
Intent intent = new Intent(
android.provider.Settings
.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
}
else {
AudioManager audioManager = (AudioManager) Settings.this.getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
}
});
this method is not working any suggestions?
UPDATE 1
i just noticed this code works but the ringer
volume volume goes down instead of media