警报管理器无法执行onReceive

时间:2014-09-24 05:51:22

标签: java android sql alarmmanager recurring

我在Android中使用Alarm Manager时遇到了一些问题。我将闹钟管理器设置为每天凌晨12点执行。这是我的代码:

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时,程序将执行插入并更新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语句。

有什么想法吗?提前谢谢。

2 个答案:

答案 0 :(得分:0)

问题在于设置警报执行的时间。用这段代码替换前三行

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

答案 1 :(得分:0)

将日历值更改为Calendar calendar = Calendar.getInstance(); calendar .set(Calendar.HOUR_OF_DAY, 0); calendar .set(Calendar.MINUTE, 0); calendar .set(Calendar.SECOND, 0);并设置闹钟。