在android中解除警报

时间:2018-03-29 16:25:24

标签: android alarm dismiss

我第一次参与广播部分并运行此应用程序时,它工作正常(警报响起)。

但是我第二次添加cancelAlarm()部分并再次运行它。它甚至没有消失。所以我猜测我之前设置的警报没有删除,因此应用程序无法获得用户new input(Alarm)

但是我不确定。我将非常感谢您的反馈。这就是我现在所拥有的。

public class MainActivity extends AppCompatActivity {

    Calendar calendar = Calendar.getInstance();
    TextView dp0;
    AlarmManager alarmManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dp0 = (TextView) findViewById(R.id.tv);
        Button sButton = (Button)findViewById(R.id.btn);
        sButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new TimePickerDialog(MainActivity.this,android.R.style.Theme_Holo_Light_Dialog,onTimeSetListener,
                        calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),true).show();
            }
        });
    }

    TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            Toast.makeText(getApplicationContext(), "Set", Toast.LENGTH_SHORT).show();

            setAlarm(calendar.getTimeInMillis());

            dp0.setText(hourOfDay + ":" + minute);
            final Button sButton1 = (Button) findViewById(R.id.cancelbtn);
            sButton1.setVisibility(View.VISIBLE);
            sButton1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dp0.setText("");
                    sButton1.setVisibility(View.INVISIBLE);
                    cancelAlarm();
                }
            });
        }
    };

    public void setAlarm(long timeInMillis) {

        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this,MyAlarm.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,intent,0);
        alarmManager.set(AlarmManager.RTC_WAKEUP,timeInMillis,pendingIntent);
    }

    public void cancelAlarm() {
        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent intent = new Intent("Myalarm");
        PendingIntent sender = PendingIntent.getBroadcast(this,0,intent,0);
        alarmManager.cancel(sender);
        sender.cancel();
        Toast.makeText(getApplicationContext(),"Canceled",Toast.LENGTH_SHORT).show();
    }
}

BroadcastReceiver

public class MyAlarm extends BroadcastReceiver {

   MediaPlayer mediaPlayer;

    @Override
    public void onReceive(Context context, Intent intent) {
         mediaPlayer = MediaPlayer.create(context, Settings.System.DEFAULT_RINGTONE_URI);
         mediaPlayer.start();
    }
}

1 个答案:

答案 0 :(得分:1)

我认为您需要使用PendingIntent.FLAG_UPDATE_CURRENT。此外,我想建议您的代码中的一些更改。

如需设置闹钟,请更改此功能setAlarm()

public void setAlarm(long timeInMillis) {
    Intent myIntent = new Intent(getApplicationContext(), MyAlarm.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
                                    getApplicationContext(), 1, myIntent, 
                                    PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    alarmManager.set(AlarmManager.RTC, timeInMillis, pendingIntent);
}

对于取消,cancelAlarm()应该是这样的。

public void cancelAlarm() {
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent myIntent = new Intent(getApplicationContext(), MyAlarm.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
                                    getApplicationContext(), 1, myIntent, 
                                    PendingIntent.FLAG_UPDATE_CURRENT);

    alarmManager.cancel(pendingIntent);
}

您可以考虑从onClickListener函数中取出sButton1 onTimeSet。将单击侦听器移动到onCreate功能。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dp0 = (TextView) findViewById(R.id.tv);
    Button sButton = (Button)findViewById(R.id.btn);
    Button sButton1 = (Button)findViewById(R.id.cancelbtn);

    sButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new TimePickerDialog(MainActivity.this,android.R.style.Theme_Holo_Light_Dialog,onTimeSetListener,
                    calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),true).show();
        }
    });

    sButton1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dp0.setText("");
            sButton1.setVisibility(View.INVISIBLE);
            cancelAlarm();
        }
    });
}

TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        Toast.makeText(getApplicationContext(), "Set", Toast.LENGTH_SHORT).show();

        setAlarm(calendar.getTimeInMillis());

        dp0.setText(hourOfDay + ":" + minute);
        sButton1.setVisibility(View.VISIBLE);
    }
};