2014-09-24 73 views
1

我遇到了Android中的Alarm Manager問題。我正在設置警報管理器,在凌晨12點的每個午夜執行一次。這裏是我的代碼:報警管理器無法執行onReceive

Calendar calendar = Calendar.getInstance(); 
calendar.setTimeInMillis(System.currentTimeMillis()); 
calendar.set(Calendar.HOUR_OF_DAY, 0); 
notificationCount = notificationCount + 1; 
AlarmManager mgr = (AlarmManager) context 
        .getSystemService(Context.ALARM_SERVICE); 
Intent notificationIntent = new Intent(context, 
        ReminderAlarm.class); 
notificationIntent.putExtra("RecurID", recurID);  
notificationIntent.putExtra("RecurStartDate", _recurlist.get(position) 
        .getRecurringStartDate()); 
notificationIntent.putExtra("Date", dateFormat.format(new Date())); 
notificationIntent.putExtra("Type", _recurlist.get(position).getRecurringType()); 
notificationIntent.putExtra("Amount", formatAmount); 
notificationIntent.putExtra("NextPaymentDate", viewHolder.txt_ddate.getText()); 
notificationIntent.putExtra("NotifyCount", notificationCount); 
PendingIntent pi = PendingIntent.getBroadcast(context, 
        notificationCount, notificationIntent, 
PendingIntent.FLAG_UPDATE_CURRENT); 
mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
        AlarmManager.INTERVAL_DAY, pi); 
ComponentName receiver = new ComponentName(context, BootReceiver.class); 
      PackageManager pm = context.getPackageManager(); 

      pm.setComponentEnabledSetting(receiver, 
        PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 
        PackageManager.DONT_KILL_APP);   
return convertView; 

當的onReceive,程序將執行INSERT和UPDATE SQL語句,如果條件符合:

ReminderAlarm類

public void onReceive(Context context, Intent intent) { 
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); 
    String recurID = intent.getStringExtra("RecurID"); 
    String recurStartDate = intent.getStringExtra("RecurStartDate"); 
    String date = intent.getStringExtra("Date"); 
    String type = intent.getStringExtra("Type"); 
    String amount = intent.getStringExtra("Amount"); 
    String nextPaymentDate = intent.getStringExtra("NextPaymentDate"); 
    String currentDate = "Next Payment On: " 
      + dateFormat.format(new Date()); 

    // If dates match then execute the SQL statements 
    if (currentDate.equals(nextPaymentDate)) { 
     DatabaseAdapter mDbHelper = new DatabaseAdapter(
       context.getApplicationContext()); 
     mDbHelper.createDatabase(); 
     mDbHelper.open(); 
     TransactionRecModel trm = new TransactionRecModel(); 
     CategoryController cc = new CategoryController(mDbHelper.open()); 

     trm.setDate(date); 
     trm.setTransDescription(description); 
     trm.setType(type); 
     trm.setAmount(Float.parseFloat(amount)); 

     // Get the categoryID based on categoryName 
     String catID = cc.getCatIDByName(categoryName); 
     trm.setCategory(catID); 

     // Check if the recurring record exists before insert new 
     // transaction record 
     RecurringController rc1 = new RecurringController(mDbHelper.open()); 
     boolean recurExist = rc1.checkRecurExist(recurStartDate, 
       description, catID); 
     if (recurExist == true) { 
      TransactionRecController trc = new TransactionRecController(
        mDbHelper.open()); 
      // Check if the transaction record exists to prevent duplication 
      boolean moveNext = trc.checkTransExist(trm); 
      if (moveNext == false) { 
       if (trc.addTransactionRec(trm)) { 
        // Update recurring start date after insertion of 
        // transaction 
        RecurringModel rm = new RecurringModel(); 
        rm.setRecurringID(recurID); 
        rm.setRecurringStartDate(date); 

        RecurringController rc = new RecurringController(
          mDbHelper.open()); 
        if (rc.updateRecurringDate(rm)) { 
         mDbHelper.close(); 
        } 
       } 
      } 
     } 
    } 

在我清單文件:

<receiver android:name="ReminderAlarm"></receiver> 
<receiver 
     android:name="BootReceiver" 
     android:enabled="false" > 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" > 
      </action> 
     </intent-filter> 
    </receiver> 

啓動接收器類:

public void onReceive(Context context, Intent i) { 
    scheduleAlarms(context); 
} 

static void scheduleAlarms(Context context) { 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 
    calendar.set(Calendar.HOUR_OF_DAY, 0); 
    calendar.set(Calendar.MINUTE, 1); 
    AlarmManager mgr = (AlarmManager) context 
      .getSystemService(Context.ALARM_SERVICE); 
    Intent notificationIntent = new Intent(context, ReminderAlarm.class); 
    PendingIntent pi = PendingIntent.getService(context, 0, 
      notificationIntent, 0); 

    mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, 
      calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi); 
} 

然而,報警管理器不會當它得到每天上午12點通過執行。插入和更新SQL語句只會在我運行應用程序時執行。

任何想法?提前致謝。

+0

您的'txt_ddate'是否包含日期字符串'「下一次付款:」'..? – Ranjit 2014-09-24 05:58:19

+0

是的。 SQL語句沒有問題,如果我從不運行應用程序,它將不會被執行。比方說,我昨天設置了一個循環,頻率是每天。當第二天上午12點,它應該執行SQL,但它沒有。它只會在第二天運行應用程序 – 2014-09-24 06:00:42

+0

@RanjitPati短句時執行它,如果我不運行應用程序,SQL將不會執行,因此我認爲是警報管理器的問題? – 2014-09-24 06:01:56

回答

1

問題在於設置警報執行的時間。與此代碼

Calendar calendar = Calendar.getInstance(); 
calendar.set(Calendar.HOUR_OF_DAY, 0); 
calendar.set(Calendar.MINUTE, 1); 
+0

它是做什麼的?設置鬧鐘管理器在上午12:01執行? – 2014-09-24 06:43:39

+0

是的,你使用calendar.setTimeInMillis(System.currentTimeMillis()); ,而不是一分鐘,所以這可能會在火警中產生問題。試試這個代碼,希望它能起作用。 – Sats 2014-09-24 06:46:31

+0

好的,但我有另外一個問題。我在ReminderAlarm類的if語句中得到了NullPointerException:if(intent.getAction()。equals(「android.intent.action.BOOT_COMPLETED」))。有任何想法嗎? – 2014-09-24 06:47:53

1
更換前三名線

日曆值更改爲Calendar calendar = Calendar.getInstance(); calendar .set(Calendar.HOUR_OF_DAY, 0); calendar .set(Calendar.MINUTE, 0); calendar .set(Calendar.SECOND, 0);,並設置報警。

+0

爲什麼需要設置分鐘和秒? – 2014-09-24 06:43:59

+0

因爲12點意味着0小時,0分和0秒,那就是爲什麼 – NIPHIN 2014-09-24 06:45:35

+0

好吧,非常感謝!但可以請你幫我再次看看這個問題,因爲我遇到了另一個問題 – 2014-09-24 06:50:11