2014-09-29 24 views
1

我在Android中的闹钟管理器遇到了一些问题。我所遇到的是,当我将闹钟设置为重复的每一分钟,它的工作原理:Android在后台运行SQL语句

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

然而,当我尝试将它设置为每天运行一次的通知,这是行不通的:

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

这是我执行报警管理器的部分。这种方法被称为在的onCreate:

public void buildListView() { 
    // Database part to retrieve the data 

    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 
    calendar.set(Calendar.HOUR_OF_DAY, 0); 
    calendar.set(Calendar.MINUTE, 1); 
     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", recurDate); 
     notificationIntent.putExtra("CurrentDate", dateFormat.format(new Date())); 
     notificationIntent.putExtra("Description", recurDesc); 
     notificationIntent.putExtra("Type", recurType); 
     notificationIntent.putExtra("Amount", formatAmount); 
     notificationIntent.putExtra("CategoryName", categoryName); 
     notificationIntent.putExtra("Frequency", frequency); 
     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); 
    } 
} 

而且从上面的方法,它会调用一个接收器:

public class BootReceiver extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent i) { 
    if (i.getAction().equals("android.intent.action.BOOT_COMPLETED")) { 
     int notificationCount = Integer.parseInt(i.getExtras() 
       .get("NotifyCount").toString()); 
     scheduleAlarms(context,notificationCount); 
    } 
} 

static void scheduleAlarms(Context context, int notificationCount) { 
    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, notificationCount, 
      notificationIntent, 0); 

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

}

然后从bootReceiver类,它会调用alarmReminder类做SQL语句和提示通知:

public class ReminderAlarm extends BroadcastReceiver { 
private NotificationManager mNotificationManager; 
private Notification notification; 

@Override 
public void onReceive(Context context, Intent intent) { 
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); 
    //Getting all the data from extras 

    Calendar cal = Calendar.getInstance(); 
    try { 
     cal.setTime(dateFormat.parse(recurStartDate)); 
     if (frequencyStr.equals("Daily")) { 
      cal.add(Calendar.DAY_OF_MONTH, 1); 
      nextPaymentDate = dateFormat.format(cal.getTimeInMillis()); 
      cal.add(Calendar.DAY_OF_MONTH, -1); 
     } else if (frequencyStr.equals("Weekly")) { 
      cal.add(Calendar.WEEK_OF_YEAR, 1); 
      nextPaymentDate = dateFormat.format(cal.getTimeInMillis()); 
      cal.add(Calendar.WEEK_OF_YEAR, -1); 
     } 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 

    // 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(currentDate); 
     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(currentDate); 

        RecurringController rc = new RecurringController(
          mDbHelper.open()); 
        if (rc.updateRecurringDate(rm)) { 
         mNotificationManager = (NotificationManager) context 
           .getSystemService(Context.NOTIFICATION_SERVICE); 
         PendingIntent contentIntent = PendingIntent 
           .getActivity(context, Integer.parseInt(intent.getExtras() 
             .get("NotifyCount").toString()), new Intent(), 0); 
         notification = new Notification(
           R.drawable.ic_launcher, "Notification", 
           System.currentTimeMillis()); 
         notification 
           .setLatestEventInfo(context, description, 
             nextPaymentDate, contentIntent); 
         mNotificationManager.notify(
           Integer.parseInt(intent.getExtras() 
             .get("NotifyCount").toString()), 
           notification); 
         mDbHelper.close(); 
        } 
       } 
      } 
     } 
    } 
} 

奇怪的是当我将它设置为每隔一分钟重复一次时,它会运行并执行插入并更新我的reminderAlarm类中的SQL。但是,当我将它设置为每天运行一次时,它不起作用。我想知道我的代码哪部分出了问题。

在此先感谢。

+0

你怎么测试呢?你可以尝试使用'setExactRepeating()'函数吗? – tasomaniac 2014-09-29 11:09:00

+0

我昨天通过添加周期性记录来测试它。但直到今天,它不会执行插入和更新提醒闹钟类中的SQL语句,直到我手动启动应用程序。它应该在后台运行 – 2014-09-29 11:16:35

+0

您可以通过更改设备时间设置来检查它。我猜你不用等一天。 – tasomaniac 2014-09-29 11:18:58

回答

0

您是否通过关闭应用程序以1分钟的间隔尝试报警,并检查数据库插入是否正在发生?

你也可以尝试改变:

DatabaseAdapter mDbHelper = new DatabaseAdapter(context.getApplicationContext()); 

DatabaseAdapter mDbHelper = new DatabaseAdapter(context); 
+0

是的,我尝试了1分钟和5分钟。首先,我设置了一个循环任务。之后,我关闭这些应用程序。这是否算关闭应用程序?奇怪的是数据库插入只在我启动应用程序时运行,它不会运行在我为闹钟管理器设置的计时器上 – 2014-09-30 10:28:00

+0

我已经测试了很多次。我意识到有一个问题。比方说,我每两小时设置一次循环重复,每次设置任务后的一分钟,它都会执行数据库插入。之后,我清除了交易记录并更改了设备的时间设置,它再次运行得非常完美。但假设我每天设置一次,在设置任务后一分钟不会提示任何通知,这意味着数据库插入不会因为日期不同而被执行。第二天,它不会运行。 – 2014-10-01 08:18:31

+0

换句话说,如果它没有执行数据库插入的一分钟我设置它,它不会运行。你有什么想法? – 2014-10-01 08:20:01