即使我按下停止按钮,闹钟仍然响起

Alarm keeps ringing even if i press the stop button

我制作了一个闹钟,它几乎可以正常工作,只是它永远不会停止。 即使我按下停止闹钟按钮,它也一直在响。

我有两个按钮,一个 ID 为 setthealarm,另一个 ID 为 stopthealarm.

在publicclassMainActivity中我定义了TimePickerPendingIntentAlarmManager

public class activity_mainnew extends Activity {
TimePicker alarmTimePicker;
    PendingIntent pendingIntent;
    AlarmManager alarmManager;



@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainnew);
alarmTimePicker = (TimePicker) findViewById(R.id.timePicker);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
}

设置时间onClick:

@RequiresApi(api = Build.VERSION_CODES.M)
    public void setthealarm(View view){
        Toast.makeText(activity_mainnew.this, "ALARM ON", Toast.LENGTH_SHORT).show();
        long time;

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getHour());
        calendar.set(Calendar.MINUTE, alarmTimePicker.getMinute());

        Intent intent = new Intent(this, AlarmReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

        alarmTimePicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);

        time = (calendar.getTimeInMillis()-(calendar.getTimeInMillis()%60000));
        if(System.currentTimeMillis()>time)
        {
            if (calendar.AM_PM == 0)
                time = time + (1000*60*60*12);
            else
                time = time + (1000*60*60*24);
        }
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 10000, pendingIntent);
    }

并停止闹钟onClick:

@RequiresApi(api = Build.VERSION_CODES.M)
public void stopthealarm(View view){
    Toast.makeText(activity_mainnew.this, "ALARM OFF", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);
    pendingIntent.cancel();
}

在我的 AlarmReceiver class 我有以下内容:

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

        if (alarmUri == null) {
        alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        }

        Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
        ringtone.play();
    }
}

知道为什么铃声不停吗?

这可能是因为您没有在这个问题中粘贴代码,但我实际上没有看到将 stopthealarm 连接到按钮 onclick 的代码。您确定 stopthealarm 功能触发了吗?

您这样做是为了 setthealarm,但不是为了 stopthealarm

setthetime onclick:

亲切的问候

要正确取消闹钟,您需要让 PendingIntent 使用 FLAG_UPDATE_CURRENT 标志。

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, AlarmReceiver.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);

同时在你的AlarmReceiver class开头添加一个请求代码行,在onReceive():

之前
public static final int REQUEST_CODE = 222;

要在单击停止按钮时在铃声上停止正在响铃的呼叫 stop()

Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.stop();