我正在尝试使用Alarm Manager每天,每周和每月重置parse int对象。我有三个对象dayObject,weekObject和monthObject,它们运行并更新文本视图。但该应用程序崩溃,错误值可能不是空。请帮忙!!!
onCreate()
:
Intent iToday = new Intent(this, TodayReset.class);
iToday.putExtra("createdAt", createdAt.getTime());
piToday = PendingIntent.getBroadcast(this, 0, iToday, 0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, today.getTime(), AlarmManager.INTERVAL_DAY, piToday);
On Receive():我今天收到来自日历的星期和月份日期。然后我检查条件并更新Parse后端。
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Pattern");
query.whereEqualTo("username", ParseUser.getCurrentUser().getUsername());
query.orderByDescending("createdAt");
query.getFirstInBackground(new GetCallback<ParseObject>() {
@Override
public void done(ParseObject object, ParseException e) {
if(e == null && object != null) {
totalValue = object.getInt("totalValue");
weekValue = object.getInt("weekValue");
monthValue = object.getInt("monthValue");
} else {
Log.i("RetErr", e.getMessage());
}
}
});
ParseObject pattern = new ParseObject("Pattern");
pattern.put("username", ParseUser.getCurrentUser().getUsername());
pattern.put("totalValue", totalValue);
pattern.put("todayValue", 0);
pattern.put("weekValue", weekValue);
pattern.put("monthValue", monthValue);
pattern.save();
我一周又一个月都在重复。我正在检索所有对象,因为当我只保存特定对象时,其他对象显示未定义。因此,我检索旧值并再次保存。
答案 0 :(得分:0)
将新Parse对象的创建移到done()中,如下所示。因为查询是在后台完成的,所以在尝试使用totalValue等之前还没有完成。
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Pattern");
query.whereEqualTo("username", ParseUser.getCurrentUser().getUsername());
query.orderByDescending("createdAt");
query.getFirstInBackground(new GetCallback<ParseObject>() {
@Override
public void done(ParseObject object, ParseException e) {
if(e == null && object != null) {
totalValue = object.getInt("totalValue");
weekValue = object.getInt("weekValue");
monthValue = object.getInt("monthValue");
ParseObject pattern = new ParseObject("Pattern");
pattern.put("username", ParseUser.getCurrentUser().getUsername());
pattern.put("totalValue", totalValue);
pattern.put("todayValue", 0);
pattern.put("weekValue", weekValue);
pattern.put("monthValue", monthValue);
pattern.save();
} else {
Log.i("RetErr", e.getMessage());
}
}
});