如何在android中以编程方式关闭警报

时间:2014-05-26 10:49:54

标签: android alarmmanager android-alarms

我有一个Android应用程序。当应用程序运行时,应该静音或禁用警报。关闭应用程序后,应再次启用警报。我用了这段代码:

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

但它仅适用于应用程序启动时。当警报满足警报时间时,它将启用。我希望在应用程序关闭之前将警报静音。我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

禁用闹钟

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

答案 1 :(得分:1)

来自AlarmManager的呼叫方法取消(...),使用与设置闹钟相同的PendingIntent。例如:

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

this.getAlarmManager().cancel(mAlarmPendingIntent);

这是指取消警报的活动或服务