我想在广播接收器中清除我的共享偏好字段电话号码,时间正好是凌晨12点。我该怎么做?这是我的密码。。。
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences prefs = context .getSharedPreferences("connect", Context.MODE_PRIVATE);
String username = prefs.getString("phonenumber", null ) ;
}
}```
报警管理器
只要报警接收器的onReceive((方法正在执行,报警管理器就会保持CPU唤醒锁定。
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。
您可以使用java.util.Timer或ScheduledThreadPoolExecutor(首选(来安排在后台线程上每隔一段时间执行一次操作。
ScheduledExecutorService scheduler =
Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate
(new Runnable() {
public void run() {
// call the preferences clear logic
}
}, 0, 10, TimeUnit.MINUTES);
EDit:
实际上,您可以节省安装时间,然后进行计算,看看是否已经过去了一周。如果已清除共享偏好
//First time
long installed = context
.getPackageManager()
.getPackageInfo(context.getPackageName(), 0)
.firstInstallTime;
参考:关于定期事件处理的更多信息获取安装时间并清除共享偏好