Search code examples
androidalarmmanager

How to effectively track number of alarm counts using AlarmManager


I had written a BroadcastReceiver as follows:

public class myBroad extends BroadcastReceiver{
int count;
@Override
public void onReceive(Context context, Intent intent) {
       PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
         PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "tgs");
             wl.acquire();
             try{
             Bundle extras = intent.getExtras();
             count=Integer.parseInt(extras.getString("status"));
             }
             catch(Exception e){
                 //nothing here
             }
             Toast.makeText(context, "value from onReceive:"+count, Toast.LENGTH_SHORT).show();
             wl.release();
     }
public void SetAlarm(Context context){
    try
    {
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
        String count=sharedPrefs.getString("list_update_count","");
        Intent in = new Intent(context, myBroad.class);
        in.putExtra("status",count);
        PendingIntent pintent = PendingIntent.getBroadcast(context, 0, in, 0);
        AlarmManager alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10*1000, pintent);
    }
    catch(Exception e)
        {
            Toast.makeText(context, e+"Here", Toast.LENGTH_SHORT).show();
        }
}
 public void CancelAlarm(Context context)
        {
            Intent intent = new Intent(context, myBroad.class);
            PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            alarmManager.cancel(sender);
        }
     } 

Iam passing the number of count(ex:5, This is the number of times that alarm must trigger) to BroadcastReciever Its working very well, But i dont know how to track the number of times when onReceive() Method fired!! Every time the OnReceive() Method calls, The count will be same(as i said:its 5).its same because all the time onReceive() method is receiving the same value:( Please help me to track the count, So that i can cancel the alarm;


Solution

  • (Edited for reusability)

    The "right" way to do this depends on what you're trying to achieve. This is one possible solution, that uses the shared preferences to keep the count at any given time:

        try{
            Bundle extras = intent.getExtras();
            int defaultCount=Integer.parseInt(extras.getString("status"));
    
            SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
            int count = sharedPrefs.getInt("current_count", defaultCount);
            if(--count == 0) {
                CancelAlarm(context);
                sharedPrefs.edit().remove("current_count").commit();
            } else {
                sharedPrefs.edit().putInt("current_count", count).commit();
            }
        }
        catch(Exception e){
            //nothing here
        }