I need to reset a value stored in sharedPreferences
at 00:00 everyday. I coded a method which does the reseting. And so far, what I'm using is a countdowntimer
that will call that method at that time. But if the app is closed/killed I don't know if the timer will keep working or not.
If not, how can I do that? Because I've read that you cant call a method using AlarmManager
, at least not easily.
Thank you so much for any help you can give me.
I skipped the code which doesn't take part in this action so you guys don't have to read through it. I want to use this alarm but the onRecieve method is never called so I don't get to reset the values I need to. Note that now the alarm is set at 10 sec for debugging.
Here is the alarm class:
public class Twelve_Oclock_Alarm2 extends BroadcastReceiver{
private final String LOG_TAG = Twelve_Oclock_Alarm2.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Log.i(LOG_TAG, "OnRecieve");
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
Log.i(LOG_TAG, "Deberia estar reseteando el valor");
SharedPreferences prefs = context.getSharedPreferences(context.getString(R.string.votes_key), Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(context.getString(R.string.votes_key), 1);
editor.commit();
wl.release();
}
public void SetAlarm(Context context){
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent("com.myrelease.duquep.you_matter.ACTION_EVENT_TIME");
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, 10000 , pi);
}
}
Here is part of my manifest:
<receiver
android:name=".Twelve_Oclock_Alarm2"
android:enabled="true"
android:process=":remote" >
<intent-filter>
<action android:name="com.myrelease.duquep.you_matter.ACTION_EVENT_TIME"/>
</intent-filter>
</receiver>
And here is where it gets called:
public class VoteActivity extends Activity {
twelve_oclock_alarm2 = new Twelve_Oclock_Alarm2();
twelve_oclock_alarm2.SetAlarm(VoteActivity.this);
}
Also know that I've tried implementing my intent also this way: (and it didnt work either)
Intent i = new Intent(context, Twelve_Oclock_Alarm2.class);
I've tried soo many things but I cant get this to work, some help would be great.