第一次启动我的应用时,我设置了一个广播。广播应该重复,但出于测试目的,我将其设置为setExact并在每次启动应用程序时调用它。 onReceive方法可以执行多项操作,并在正确的时间执行它们。但是当我注册广播(在它被触发之前)并且我无法弄清楚原因时,onReceive中的方法(即向数据库添加新行)被立即调用。因此,每次启动应用程序时,它都会创建一个新条目,每次onReceive都会被调用,它会生成另一个(正确的)条目。
我在启动应用程序时调用此方法来设置BroadcastReceiver。如果时间过去,我会向Day添加+1,然后在第二天调用。
public void startDayCheckOnce() {
registerReceiver(new DayCheckReceiver(), new IntentFilter(DAY_CHECK_RECEIVER));
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
dayCheckIntent = new Intent(DAY_CHECK_RECEIVER);
sendBroadcast(dayCheckIntent);
dayCheckAlarmIntent = PendingIntent.getBroadcast(this, 2000000000, dayCheckIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 40);
if (calendar.before(Calendar.getInstance())) {
calendar.add(Calendar.DATE, 1);
}
//TODO: SET BACK TO SETREPEATING
alarmManager.setExact(AlarmManager.RTC, calendar.getTimeInMillis(), dayCheckAlarmIntent);
dayCheckStarted = true;
saveDayCheckStarted();
}
这是BroadcastReceiver。一切都在正确的时间被调用,只有
mDbHelper.addDayCheck()
在应用程序开始时立即调用:
public class DayCheckReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (null != intent && DAY_CHECK_RECEIVER.equals(intent.getAction())) {
//load TaskList
SharedPreferences sharedPrefs = getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPrefs.getString(TASKLIST_FILE, null);
Type type = new TypeToken<ArrayList<TaskItem>>() {
}.getType();
mTaskList = gson.fromJson(json, type);
//modifications && writing to database
if (mTaskList != null && mTaskList.size() > 0) {
DatabaseHelper mDbHelper = new DatabaseHelper(context);
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -1);
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
String date = df.format(c.getTime());
String millisIdentifier;
int doneState;
for (int i = 0; i < mTaskList.size(); i++) {
millisIdentifier = mTaskList.get(i).getMilliIdentifier();
doneState = mTaskList.get(i).getDoneTodayValue();
mDbHelper.addDayCheck(millisIdentifier, date, doneState);
mTaskList.get(i).shiftDayChecks();
mTaskList.get(i).resetTimeLeft();
}
}
//save TaskList
SharedPreferences.Editor editor = sharedPrefs.edit();
gson = new Gson();
json = gson.toJson(mTaskList);
editor.putString(TASKLIST_FILE, json);
editor.apply();
//refresh Fragments
if (tasksFragment.isVisible()) {
FragmentTransaction refreshFT = getSupportFragmentManager().beginTransaction();
refreshFT.detach(tasksFragment);
refreshFT.attach(tasksFragment);
refreshFT.commit();
} else if (timerFragment.isVisible()) {
timerFragment.refreshUI();
}
}
}
}
这是dayCheck方法本身:
public void addDayCheck(String millisIdentifier, String date, int doneState){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_DATE, date);
values.put(COLUMN_DONE_STATE, doneState);
db.insert(millisIdentifier, null, values);
}
修改
好的,原因似乎是“sendBroadcast(dayCheckIntent)”。我根据教程构建了它并且必须做错了,因为这种方法显然会立即调用广播。
答案 0 :(得分:0)
好的,我确实发现了错误。我在设置广播时调用了“sendBroadcast(dayCheckIntent)”,并立即调用onReceive。这似乎是一个简单的错误,但由于交叉不同的其他事情,似乎onReceive只被调用一次,但实际上并非如此