Android alarm not cancelling

笑着哭i 提交于 2019-12-11 19:33:39

问题


I am in the main activity. There is a Login button bLogin. When it is pressed, a Logout button is displayed bLogout. The onClick methods for the two buttons are as follows:

bLogin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        llLogin.setVisibility(View.GONE);
        llLogout.setVisibility(View.VISIBLE);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 327,
            new Intent(getApplicationContext(), AlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
    }
});

bLogout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        llLogout.setVisibility(View.GONE);
        llLogin.setVisibility(View.VISIBLE);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 327,
            new Intent(getApplicationContext(), AlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        manager.cancel(pendingIntent);

        boolean alarmUp = (PendingIntent.getBroadcast(getApplicationContext(), 327,
            new Intent(getApplicationContext(), AlarmReceiver.class), PendingIntent.FLAG_NO_CREATE) != null);
        if(!alarmUp){
            Toast.makeText(getBaseContext(), "up", Toast.LENGTH_SHORT).show();
        }
    }
});

As can be seen in the code above, when bLogin is pressed, I set the alarm, and when bLogout is pressed, I cancel the alarm.

alarmUp is used to check if the alarm is set. But the problem is that the alarm is never cancelled because the Toast at the end is never displayed. Also, the work that should be done by the app when the alarm is not set is never done on pressing Logout.

I can't seem to figure out what might be wrong. The PendingIntents are the same for both when the alarm is set, and when it is cancelled.


回答1:


Don't know what's wrong with your code, but if you want to cancel the pending intent you can use

PendingIntent.getBroadcast(getActivity(), 327, pendingIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT).cancel();

to cancel the pending intent. I think it'll do the same think as cancelling your AlarmManager. Although you might need to change the way you check the way your alarm is set.

Hope this helps.




回答2:


You aren't cancelling the PendingIntent. When you call

manager.cancel(pendingIntent) 

you are cancelling the alarm. This doesn't cancel the PendingIntent. The PendingIntent still exists. So when you then call

boolean alarmUp = (PendingIntent.getBroadcast(getApplicationContext(), 327,
        new Intent(getApplicationContext(), AlarmReceiver.class),
        PendingIntent.FLAG_NO_CREATE) != null);

the PendingIntent still exists, so PendingIntent.getBroadcast() will return a non-null. result. alarmUp will always be true.

You need to cancel the PendingIntent after you cancel the alarm, like this:

pendingIntent.cancel();


来源:https://stackoverflow.com/questions/30821956/android-alarm-not-cancelling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!