我为Android中的通知制作了警报器。但是,如何在Android中制作“停止按钮”?启动警报的代码:

public void playRingtone(View view) {
    try {
        Uri path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.song);
        RingtoneManager.setActualDefaultRingtoneUri(
                getApplicationContext(), RingtoneManager.TYPE_ALARM, path
        );
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), path);
        r.play();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

最佳答案

 RingtoneManager ringtoneManager = new RingtoneManager(getContext());

 public void playRingtone(View view) {
    try {
        Uri path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.song);
        ringtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_ALARM, path);
        Ringtone r = ringtoneManager.getRingtone(getApplicationContext(), path);
        r.play();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void stopRingtone(){
   ringtoneManager.stopPreviousRingtone();
}

07-27 19:51