如何在Android中以编程方式关闭闹钟

5

我有一个安卓应用程序。当应用程序正在运行时,闹钟应该被静音或禁用。在关闭应用程序后,闹钟应该再次启用。我使用了这段代码:

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个回答

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

需要解释一下。要点是什么?主意是什么? - Peter Mortensen

1

从AlarmManager调用cancel(...)方法,使用设置闹钟时使用的相同PendingIntent。示例:

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

this.getAlarmManager().cancel(mAlarmPendingIntent);

这指的是您要取消闹钟的活动或服务。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接