我在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。但是,当我将它设置为每天运行一次时,它不起作用。我想知道我的代码哪部分出了问题。
在此先感谢。
你怎么测试呢?你可以尝试使用'setExactRepeating()'函数吗? – tasomaniac 2014-09-29 11:09:00
我昨天通过添加周期性记录来测试它。但直到今天,它不会执行插入和更新提醒闹钟类中的SQL语句,直到我手动启动应用程序。它应该在后台运行 – 2014-09-29 11:16:35
您可以通过更改设备时间设置来检查它。我猜你不用等一天。 – tasomaniac 2014-09-29 11:18:58