2014-09-27 141 views
1

我在尝试取消Android中的闹钟管理器时遇到了一些问题。我打电话给这个retrieveBudget()onCreate:在Android中取消闹钟管理器

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类,它只是简单地设置通知。所以我的问题是它每分钟都执行通知。但在将提醒通知更改为“N”而不是“Y”后,它不会取消警报管理器。我不知道为什么会这样,因为在更新SQL语句后,我再次调用retrieveBudget()。

在此先感谢。

编辑

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

您的个人信息被初始化为空,如果你还想要取消你必须重新创建您的待定意图,然后通过在AlarmManager上取消发送 – Pavlos 2014-09-27 08:55:11

+0

好吧,我只是复制了四行代码? – 2014-09-27 08:58:41

+0

是的,请确保它与您发送的相同! – Pavlos 2014-09-27 08:59:56

回答

2

通知计数,而在初始化报警经理的时间创作和消除悬而未决的意图应该是相同的使用的价值。

在挂起的意图中使用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); 
    } 

,如果你改变requestFlag的值(在你的情况notificationcount),同时启动报警后初始化未决的意图,你不会能够取消它。

在创建和取消警报时,在挂起的意图中使用相同的requestFlag值。在这里,我正在使用1.

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

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

检查下面的链接:

的PendingIntent getBroadcast(上下文的背景下,INT requestCode,意图意图,诠释标志)

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

+0

因此,您的意思是我应该在声明PendingIntent之后并在if语句之前提出该声明? – 2014-09-27 10:11:21

+0

请您详细说明一下吗?我仍然感到困惑 – 2014-09-27 10:18:51

+0

好吧,非常感谢!所以对于我的情况,我应该用notificationCount替换1。另外,你为什么使用1?对不起,我是高级的混淆 – 2014-09-27 10:30:07

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

对不起,但我该怎么办? – 2014-09-27 09:34:29

+0

只需将其替换为您设置闹钟管理器的旧代码 – Pavlos 2014-09-27 09:36:19

+0

不可以,它不起作用。警报管理器仍在执行。你有什么想法,为什么? – 2014-09-27 09:43:17