gpt4 book ai didi

java - 在 Android 中取消警报管理器

转载 作者:行者123 更新时间:2023-11-30 02:38:54 24 4
gpt4 key购买 nike

我在尝试取消 Android 中的 AlarmManager 时遇到了一些问题。我调用这个 retrieveBudget() onCreate:

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();
}

在我的 budgetAlarm 类中,它只是简单地设置通知。所以我的问题是它确实每分钟执行一次通知。但是在我将我的 reminderNotify 更改为“N”而不是“Y”之后,它并没有取消警报管理器。我想知道为什么会这样,因为在更新 SQL 语句之后,我再次调用此 retrieveBudget()。

提前致谢。

编辑

这些是我更新预算部分的代码。当单击 Okay from 对话框时,我再次调用 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();
}

最佳答案

在警报管理器的创建和取消时初始化未决 Intent 时使用的通知计数值应该相同。

在未决 Intent 中使用相同的 requestFalg 值。

替换这部分:

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);
}

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);
}

如果您在启动警报后初始化未决 Intent 时更改了 requestFlag 的值(在您的情况下为 notificationcount),您将无法取消它。

在创建和取消警报时在未决 Intent 中使用相同的 requestFlag 值。在这里,我使用 1。

由于您是在启动警报管理器后更改 notificationFlag 的值,因此不要将 notificationFlag 用作 requestFlag。在 if 和 else 部分中初始化 pi 时,使用常量整数值作为 requestFlag。

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

检查下面的链接:

PendingIntent getBroadcast (Context context, int requestCode, Intent Intent , int flags)

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

关于java - 在 Android 中取消警报管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26072976/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com