5

I have an Android application. When the app is running, the alarm should be muted or disabled. After closing the application, the alarm should be enabled again. I used this code:

AudioManager AudiMngr = (AudioManager) getSystemService(AUDIO_SERVICE);
AudiMngr.setRingerMode(AudioManager.RINGER_MODE_SILENT);

AudiMngr.setStreamVolume(AudioManager.STREAM_ALARM, 0, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);

Toast toast = Toast.makeText(getApplicationContext(), "Sound Muted", Toast.LENGTH_SHORT);
toast.show();

But it works only at the time of application starting. When the alarm meets the alarm time, it is enabled. I wish to mute the alarm until the application closes. How can I do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Parthi
  • 669
  • 1
  • 9
  • 26

2 Answers2

2

To disable an alarm

AlarmManager aManager = (AlarmManager) getSystemService(ALARM_SERVICE);         
Intent intent = new Intent(getBaseContext(), YourAlarmSetClass.class);      
PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);         
aManager.cancel(pIntent);
Saqib
  • 1,120
  • 5
  • 22
  • 40
1

Call method cancel(...) from AlarmManager, using the same PendingIntent you used to set the alarm. Example:

mAlarmPendingIntent = PendingIntent.getActivity(this, requestCode, intent, flags);

this.getAlarmManager().cancel(mAlarmPendingIntent);

This refers to the Activity or the Service from which you are cancelling the alarm.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131