2

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Pablo Duque
  • 383
  • 2
  • 20
  • Take a look at the `ScheduledExecutorService`: http://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService.html – Freek Wiekmeijer Nov 03 '15 at 09:50

1 Answers1

0

Using AlarmManager you can through a Broadcast at 00:00 (your desired time) and in your receiver you can reset you sharedPreferences values, not very hard to do.

For more details on AlarmManager refer the following: this and that

Alarm Manager Example

Community
  • 1
  • 1
Nitesh
  • 3,868
  • 1
  • 20
  • 26
  • @dummy I followed what you said Nitesh but I cant get the onReceive method to be called. I'll edit the question to show u all the code. Thanks – Pablo Duque Nov 08 '15 at 11:34
  • can you think of any solution to the code I added? Thanks – Pablo Duque Nov 09 '15 at 17:46
  • here is a simplest example of what you want to do. do exactly what it says. Firstly run this as such then try to change am.setRepeating according to your need http://karanbalkar.com/2013/07/tutorial-41-using-alarmmanager-and-broadcastreceiver-in-android/ – Nitesh Nov 10 '15 at 03:03