无法弄清楚如何为敲响创建取消按钮(Cannot figure out how to create t

2019-10-29 15:01发布

我有从我按下了按钮stopAlarm即使振铃停止报警麻烦。 我无法弄清楚如何获得访问相同的铃声实例,并调用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();
}
}

Answer 1:

脚步:

添加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的,你是immediatelly试图访问inst.setAlarmText方法。 你这一翻译可以使用本地广播或服务绑定更新您的UI -发送广播/邮件到您的Activity从工作RingtonePlayingService与启动/停止/当前状态



文章来源: Cannot figure out how to create the cancel button for ringing alarm