2014-09-24 139 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