0

所以我得到了这个警报管理器,它可以由带有 BOOT_COMPLETED 的 intentService 触发。

警报管理器的工作是每 10 分钟调用另一个意图服务。此意图服务更新 SQLite 数据库值。

7:00  = boot time, alarm manager set up
7:10 = intent service called, increment a value in SQLITE, "Week" column, from value 1 to 2
7:20 = same as above, value 2 to 3
7:21 = turn off the phone..
7:22 = turn on the phone, set up the alarm manager again
7:30 = same as 7:10 and 7:20 function, 3 to 4
7:31 = turn off again
7:55 = turn on again, the function that should be off twice, (7:40 and 7:50) but it only goes off once, meaning the value was change from 4 to 5, instead of 4 to 5 to 6..

每周的每一次变化,意图服务都会根据数量做其他事情,我永远不应该跳过任何一周

@Override
public void onReceive(Context context, Intent intent) {
    PowerManager pm = (PowerManager) context
            .getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(
            PowerManager.PARTIAL_WAKE_LOCK, "");
    wl.acquire();

    // Put here YOUR code.
    Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For
                                                                            // example

    wl.release();
    context.startService(new Intent(context, CheckClassroom.class));

    SharedPreferences a = context.getSharedPreferences("mPref", 0);
    SharedPreferences.Editor editor = a.edit();
    long interval = System.currentTimeMillis();
    editor.putLong("first", interval + (30 * 1000));
    int idnow = a.getInt("idnow", 1);
    idnow = idnow + 1;
    editor.putInt("idnow", idnow);

    editor.commit();

}

public void SetAlarm(Context context) {
    SharedPreferences a = context.getSharedPreferences("mPref", 0);

    long iFirst = a.getLong("first", System.currentTimeMillis()
            + (30 * 1000));


    AlarmManager am = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, Alarm.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);

    am.setRepeating(AlarmManager.RTC_WAKEUP, iFirst, 30 * 1000, pi); // 
}
4

0 回答 0