Alarmreceiver如何停止媒体播放器

时间:2015-06-21 17:38:15

标签: android android-activity

AlarmReceiver.class

private void setAlarm(Calendar targetCal) {
        textAlarmPrompt.setText(
                "\n\n***\n"
                        + "Alarm is set@ " + targetCal.getTime() + "\n"
                        + "***\n");

        Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

    }

这会在达到计时器时设置闹钟,然后播放音频。

以下此代码来自Mainactivity。

 private void cancelAlarm() {

            textAlarmPrompt.setText(
                    "\n\n***\n"
                            + "Alarm Cancelled! \n"
                            + "***\n");




            Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarmManager.cancel(pendingIntent);


        }

停止闹钟:但当此代码触发 AlarmReceiver.class 时,音频不会停止。

以下代码来自Mainactivity

binding.pry

1 个答案:

答案 0 :(得分:0)

提供服务来处理警报,您可以通过在startService中设置警报计划并在stopService中停止来处理它