I have a class notifer that is running in the background. It restarts itself after every 1 min. onReceive of notifer is:
@Override
public void onReceive(final Context contextn, final Intent intent)
{
Log.d("Notifier","started");
String id[] = intent.getStringArrayExtra("id");
Log.d("notifier",String.valueOf(id.length));
if (id!=null) {
context = contextn;
backgroundsync back = new backgroundsync();
back.execute(id);
Intent callagain = new Intent(context, notifer.class);
callagain.putExtra("id", id);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, callagain, 0);
AlarmManager notifalm;
notifalm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
notifalm.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 60000, alarmIntent);
}
}
Whenever there is a change in data that notifer need to work on I do this from UI thread when the data change is detected.
cancelalarm();
//changed data in database here
setalarm();
method definition is :
private void setalarm()
{
Intent callagain = new Intent(this,notifer.class);
String ids[]=interact.checked();
for (int i=0;i<ids.length;i++)
Log.d("Main checked new",ids[i]);
callagain.putExtra("id", ids);
alarmIntent = PendingIntent.getBroadcast(this, 0, callagain, 0);
AlarmManager notifalm;
notifalm = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
notifalm.setExact(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),alarmIntent);
}
private void cancelalarm()
{
Intent callagain = new Intent(this,notifer.class);
String ids[]=interact.checked();
for (int i=0;i<ids.length;i++)
Log.d("Main checked old",ids[i]);
callagain.putExtra("id", ids);
alarmIntent = PendingIntent.getBroadcast(this, 0, callagain, 0);
AlarmManager notifalm;
notifalm = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
notifalm.cancel(alarmIntent);
}
interact.checked() returns data from database that needs to be sent to notifer.
The problem is even when data is changed the notifer still keeps working on old data. Pls, help I am stuck here for hours.
Let me know if you need any other info.