简体   繁体   English

在Android中取消警报管理器

[英]Cancelling Alarm Manager in Android

I am having some problem when trying to cancel AlarmManager in Android. 尝试在Android中取消AlarmManager时遇到问题。 I am calling this retrieveBudget() onCreate: 我在onCreate上将其称为retrieveBudget():

public void retrieveBudget() {
    txtDisplayMonthlyExpenses = (TextView) findViewById(R.id.txtDisplayMonthlyExpense);
    txtDisplayBudget = (TextView) findViewById(R.id.txtDisplayBudget);
    cbDisplayReminderNotify = (CheckBox) findViewById(R.id.cbDisplayReminderNotify);
    cbDisplayReminderNotify.setEnabled(false);

    DatabaseAdapter mDbHelper = new DatabaseAdapter(this);
    mDbHelper.createDatabase();
    mDbHelper.open();
    TransactionRecController trc = new TransactionRecController(
            mDbHelper.open());
    currentMonthExpense = trc.getThisMonthDebit();
    txtDisplayMonthlyExpenses.setText("$ "
            + formatter.format(currentMonthExpense));

    BudgetController bc = new BudgetController(mDbHelper.open());

    BudgetModel bm = bc.getBudget();
    if (bm.getBudgetAmount() != 0 && bm.getReminderNotify() != null) {
        budget = bm.getBudgetAmount();
        txtDisplayBudget.setText("$ " + formatter.format(budget));
        if (bm.getReminderNotify().equals("Y")) {
            cbDisplayReminderNotify.setChecked(true);
        } else {
            cbDisplayReminderNotify.setChecked(false);
        }
        AlarmManager mgr = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        PendingIntent pi = null;
        if (bm.getReminderNotify().equals("Y")
                && currentMonthExpense > budget) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 1);
            notificationCount = notificationCount + 1;

            Intent notificationIntent = new Intent(context,
                    BudgetAlarm.class);
            notificationIntent.putExtra("NotifyCount", notificationCount);
            pi = PendingIntent.getBroadcast(context, notificationCount,
                    notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                    calendar.getTimeInMillis(), 60000, pi);
        } else {
            mgr.cancel(pi);
        }
    } else {
        txtDisplayBudget.setText("No budget record yet");
    }

    mDbHelper.close();
}

And inside my budgetAlarm class, it just simply setting the notification. 在我的budgetAlarm类中,只需设置通知即可。 So my problem is it did execute the notification every minute. 所以我的问题是它确实每分钟执行一次通知。 But after I changed my reminderNotify to "N" instead of "Y", it does not cancel the alarm manager. 但是,在将我的notifyNotify更改为“ N”而不是“ Y”之后,它并没有取消警报管理器。 I wonder why is it so because after my update SQL statement, I am calling this retrieveBudget() again. 我不知道为什么会这样,因为在更新SQL语句之后,我再次调用了restoreBudget()。

Thanks in advance. 提前致谢。

EDIT 编辑

These are the codes where I update the budget part. 这些是我更新预算部分的代码。 I am calling the the retrieveBudget() once again when the Okay from dialogue box was clicked. 当单击“来自对话框的确定​​”对话框时,我再次调用retrieveBudget()。

public void onEditBudgetClicked(View view) {
    AlertDialog.Builder EditDialog = new AlertDialog.Builder(this);
    EditDialog.setTitle("Edit Budget");

    // Get the components from XML file
    LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View dialogView = li.inflate(R.layout.edit_budget, null);
    txtEditBudgetAmount = (EditText) dialogView
            .findViewById(R.id.txtEditBudgetAmount);
    cbEditReminderNotify = (CheckBox) dialogView
            .findViewById(R.id.cbEditReminderNotify);

    // Retrieve budget record
    DatabaseAdapter mDbHelper = new DatabaseAdapter(this);
    mDbHelper.createDatabase();
    mDbHelper.open();
    BudgetController bc = new BudgetController(mDbHelper.open());
    BudgetModel bm = bc.getBudget();
    if (bm.getBudgetAmount() != 0 && bm.getReminderNotify() != null) {
        txtEditBudgetAmount.setText(Float.toString(bm.getBudgetAmount()));
        if (bm.getReminderNotify().equals("Y")) {
            cbEditReminderNotify.setChecked(true);
            editReminderNotify = "Y";
        } else {
            cbEditReminderNotify.setChecked(false);
        }
    }
    mDbHelper.close();

    cbEditReminderNotify.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (cbEditReminderNotify.isChecked()) {
                editReminderNotify = "Y";
            } else {
                editReminderNotify = "N";
            }
        }
    });

    EditDialog.setView(dialogView);
    EditDialog.setPositiveButton("Ok",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    onEditBudgetSubmitClicked();
                    dialog.dismiss();
                    retrieveBudget();
                }
            });

    EditDialog.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.dismiss();
                }
            });
    EditDialog.show();
}

The value of notification count used while initializing the pending intent at the time creation and cancellation of the alarm manager should be same. 在创建和取消警报管理器时初始化未决意图时使用的通知计数值应相同。

Use same value of requestFalg in the pending intent. 在待处理的意图中使用相同的requestFalg值。

Replace this part: 替换此部分:

AlarmManager mgr = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = null;
if (bm.getReminderNotify().equals("Y")
           && currentMonthExpense > budget) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 1);
    notificationCount = notificationCount + 1;
    Intent notificationIntent = new Intent(context,
                BudgetAlarm.class);
    notificationIntent.putExtra("NotifyCount", notificationCount);
    pi = PendingIntent.getBroadcast(context, notificationCount,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                calendar.getTimeInMillis(), 60000, pi);
    } else {
        mgr.cancel(pi);
    }

with

AlarmManager mgr = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(context,
                BudgetAlarm.class);
PendingIntent pi = null;
if (bm.getReminderNotify().equals("Y")
            && currentMonthExpense > budget) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 1);
    notificationCount = notificationCount + 1;
    notificationIntent.putExtra("NotifyCount", notificationCount);
    pi= PendingIntent.getBroadcast(context, 1,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                calendar.getTimeInMillis(), 60000, pi);
    } else {
        pi= PendingIntent.getBroadcast(context, 1,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mgr.cancel(pi);
    }

if you changed the value of requestFlag (in your case notificationcount)while initializing the pending intent after launching the alarm, you won't be able to cancel it. 如果在启动警报后初始化挂起的意图时更改了requestFlag的值(在您的情况下为notificationcount),则将无法取消它。

Use same value of requestFlag in the pending intent while creating and cancelling the alarm. 创建和取消警报时,在挂起的意图中使用相同的requestFlag值。 Here, I am using 1. 在这里,我正在使用1。

Since you are changing the value of notificationFlag after launching the alarm manager, don't use notificationFlag as a requestFlag. 由于在启动警报管理器后要更改notificationFlag的值,因此请勿将notificationFlag用作requestFlag。 Use a constant integer value as a requestFlag while initializing pi inside both if and else part. 在if和else部分中初始化pi时,请使用常量整数值作为requestFlag。

pi= PendingIntent.getBroadcast(context, CONSTANT_INTEGER,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); pi = PendingIntent.getBroadcast(context,CONSTANT_INTEGER,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);

check the link below: 检查以下链接:

PendingIntent getBroadcast (Context context, int requestCode, Intent intent, int flags) PendingIntent getBroadcast(上下文上下文,int requestCode,Intent意图,int标志)

http://developer.android.com/reference/android/app/PendingIntent.html#getBroadcast%28android.content.Context,%20int,%20android.content.Intent,%20int%29 http://developer.android.com/reference/android/app/PendingIntent.html#getBroadcast%28android.content.Context,%20int,%20android.content.Intent,%20int%29

    AlarmManager mgr = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    PendingIntent pi = null;
    if (bm.getReminderNotify().equals("Y")
            && currentMonthExpense > budget) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 1);
        notificationCount = notificationCount + 1;

        Intent notificationIntent = new Intent(context,
                BudgetAlarm.class);
        notificationIntent.putExtra("NotifyCount", notificationCount);
        pi = PendingIntent.getBroadcast(context, notificationCount,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                calendar.getTimeInMillis(), 60000, pi);
    } else {
       notificationCount = notificationCount + 1;

        Intent notificationIntent = new Intent(context,
                BudgetAlarm.class);
        notificationIntent.putExtra("NotifyCount", notificationCount);
        pi = PendingIntent.getBroadcast(context, notificationCount,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mgr.cancel(pi);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM