简体   繁体   English

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

[英]How to turn off an alarm programmatically on Android

I have an Android application.我有一个 Android 应用程序。 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?我怎样才能做到这一点?

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

Call method cancel(...) from AlarmManager, using the same PendingIntent you used to set the alarm.从 AlarmManager 调用方法 cancel(...),使用与设置警报相同的 PendingIntent。 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.这是指您要取消警报的活动或服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM