2014-09-27 92 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