0

アプリケーションで db から一部のデータを削除するために SQL クエリを起動しようとしています。ポイントは、SQLクエリが指定された期間(1日または2日など)後に起動することです。その間、アプリケーションはアイドル状態になる可能性があります...

4

1 に答える 1

0

Android AlarmManagerを使用する必要があります。

必要に応じて特定の時間に起動されるレシーバーをスケジュールし、onReceive でデータベースから削除するためのコードを記述します

次のようにアラームを設定します

Intent intent = new Intent(context, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent

);

受信側で削除用のコードを書く

public class AlarmReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
        // code to delete from db
            }

    }
于 2012-11-05T10:00:48.657 に答える