Alarm Manager
The Alarm Manager holds a CPU wake lock as long as the alarm receiver's onReceive() method is executing.
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
synchronized public void run() {
preferences.edit().putString(key, "").apply()
}
}, TimeUnit.MINUTES.toMillis(1), TimeUnit.MINUTES.toMillis(1));
ScheduledThreadPoolExecutor.
You can use java.util.Timer or ScheduledThreadPoolExecutor (preferred) to schedule an action to occur at regular intervals on a background thread.
ScheduledExecutorService scheduler =
Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate
(new Runnable() {
public void run() {
// call the preferences clear logic
}
}, 0, 10, TimeUnit.MINUTES);
EDit:
You can actually save the install time and then do a calculation to see if a week has elapsed. If it has clear the shared preference
//First time
long installed = context
.getPackageManager()
.getPackageInfo(context.getPackageName(), 0)
.firstInstallTime;
Ref:More on periodic event handling
Get install time and clear shared preference