无法弄清楚如何为警报创建取消按钮

时间:2019-03-09 12:12:07

标签: java android android-intent alarm android-alarms

即使按了stopAlarm按钮,也无法阻止警报响起。我无法弄清楚如何访问相同的Ringtone实例并调用stop()。

这是MainActivity java类中的启动警报开关。

public void switchClicked(View view) {
    if (((Switch) view).isChecked()) {
        Log.d("MainActivity", "Alarm On");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getHour());
        calendar.set(Calendar.MINUTE, alarmTimePicker.getMinute());
        Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);
        alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
        setAlarmText("ON");


    } else {
        alarmManager.cancel(pendingIntent);
        setAlarmText("OFF");
        Log.d("MainActivity", "Alarm Off");
    }
}

public void setAlarmText(String alarmText) {
    alarmTextView.setText(alarmText);
}

这是MainActivity java类中的StopAlarm按钮。

public void stopAlarm(View view) {
        setAlarmText("Alarm stopped");
        Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager.cancel(pendingIntent);
    }

这是AlarmReciver Java类。

public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {

    MainActivity inst = MainActivity.instance();
    inst.setAlarmText("Alarm! Wake up! Wake up!");
    Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    if (alarmUri == null) {
        alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    }
    Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
    ringtone.play();
}
}

1 个答案:

答案 0 :(得分:0)

步骤:

RingtonePlayingService添加到您的项目中(不要忘记在清单中声明它)

public class RingtonePlayingService extends Service{

    private Ringtone ringtone = null;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        if (alarmUri == null) 
            alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        this.ringtone = RingtoneManager.getRingtone(this, alarmUri);
        ringtone.play();

        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent){
        return null;
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        shutUpRingtone();
    }

    private void shutUpRingtone(){
        if(ringtone != null)
            ringtone.stop();
        ringtone = null;
    }
}

删除onReceive中包含的代码,并将其放入:

@Override
public void onReceive(final Context context, Intent intent) {
    Intent startIntent = new Intent(context, RingtonePlayingService.class);
    context.startService(startIntent);
}

现在RingtonePlayingService正在处理铃声播放并保持引用。您的stopAlarm方法只是取消潜在的未决警报,因此,如果在单击“停止”按钮之前还没有触发,它就永远不会触发。但是,当它已经开始响铃时,您应该停止已启动服务持有的ringtone实例。在方法末尾添加:

public void stopAlarm(View view) {
    ...
    Intent stopIntent = new Intent(context, RingtonePlayingService.class);
    stopService(stopIntent);
}

现在stopAlarm可以同时执行以下操作:取消挂起的警报(如果有)并停止服务(如果有生命),这可能正在播放铃声

还要删除MainActivity.instance()模式,这真是太糟糕了……当您设置将来的警报(例如now + 1h)并且系统(或用户)在应用被触发之前将其从内存中删除时,您将得到{ {1}},因为NullPointerException.instance(),因此您立即尝试访问null方法。您的Intead可能会使用Local Broadcastingservice binding来更新您的用户界面-通过inst.setAlarmText的开始/停止/当前状态来向您的Activity发送广播/消息