[英]How to turn off an alarm programmatically on Android
我有一個 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();
但它僅在應用程序啟動時有效。 當鬧鍾達到鬧鍾時間時,它被啟用。 我希望在應用程序關閉之前將鬧鍾靜音。 我怎樣才能做到這一點?
禁用鬧鍾
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);
從 AlarmManager 調用方法 cancel(...),使用與設置警報相同的 PendingIntent。 例子:
mAlarmPendingIntent = PendingIntent.getActivity(this, requestCode, intent, flags);
this.getAlarmManager().cancel(mAlarmPendingIntent);
這是指您要取消警報的活動或服務。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.