即使我按下停止按钮,警报也会响起

时间:2016-12-29 00:02:10

标签: android ringtonemanager

我制作了一个闹钟,它几乎正常工作,但它永远不会停止。 即使我按下我停止响铃的闹钟按钮。

我有两个按钮,一个是id setthealarm ,另一个是id stopthealarm

在公共课MainActivity中,我定义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类中,我有以下内容:

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

请问为什么铃声不会停止?

2 个答案:

答案 0 :(得分:0)

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

您为 setthealarm 执行此操作,但不为 stopthealarm

执行此操作
setthetime onclick:

亲切的问候

答案 1 :(得分:0)

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

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

还要在onReceive()之前的AlarmReceiver类中添加请求代码行:

public static final int REQUEST_CODE = 222;

要在点击停止按钮时停止铃声上的铃声stop()

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