无法弄清楚如何创建取消按钮以响铃

问题描述 投票:0回答:1

即使在按下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();
}
}
java android android-intent alarm android-alarms
1个回答
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()模式,这是坏的地狱......当你设置未来警报(例如现在+ 1h)和系统(或用户)从内存中删除应用程序之前,你会得到NullPointerException,因为.instance()null,并且你是立即尝试访问inst.setAlarmText方法。您可以使用Local Broadcastingservice binding更新您的用户界面 - 使用开始/停止/当前状态从Activity发送广播/消息到您的RingtonePlayingService

© www.soinside.com 2019 - 2024. All rights reserved.