samedi 14 février 2015

How to update a column only once per day in SQLite using alarm manager in Android?

I have a scenario in which I want to update a column in SQLite database that has the difference between a user selected date and today (i.e remaining days more for a user selected day). Now I want to update this value daily so that it gets updated (or decremented). I have tried the following, but I am not able to update the column successfully (i.e the values are not getting decremented but the notification comes). I am getting a notification repeatedly, but what I want is to get that notification once. I am putting a notification to check whether my alarm works properly or not. This will be removed if it works properly. So, how to update the remaining days column only once daily using alarm manager in android? Also, Is this method correct to update a column in sqlite daily? Please help. Thanks.



scheduleUpdate() in MainActivity.java



private void scheduleUpdate() {
Calendar update_alarm = Calendar.getInstance();
update_alarm.setTimeInMillis(System.currentTimeMillis());
update_alarm.set(Calendar.HOUR_OF_DAY, 0);
Intent intent = new Intent(getApplicationContext(), MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1, intent, 0);
am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, update_alarm.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
}


UpdateRemainingDays.java



public class UpdateRemainingDays extends Service {
private DBadapter myDb;
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
openDB();
Intent intent = new Intent(this, MainActivity.class);
long[] pattern = {0, 100, 0};
PendingIntent pi = PendingIntent.getActivity(this, 1, intent,0);

myDb.update_column();

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Updating")
.setVibrate(pattern)
.setAutoCancel(true);
mBuilder.setContentIntent(pi);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(01234, mBuilder.build());

}
@Override
public void onDestroy() {
super.onDestroy();
closeDB();
}
private void openDB() {
myDb = new DBadapter(this);
myDb.open();
}
private void closeDB() {
myDb.close();
}
}


MyReceiver.java



public class MyReceiver extends BroadcastReceiver
{

@Override
public void onReceive(Context context, Intent intent)
{
Intent serviceUpdate = new Intent(context, UpdateRemainingDays.class);
context.startService(serviceUpdate);
}
}


update_column() in DBadapter.java



public void update_column() {
String where = "UPDATE "+ MY_TABLE +" SET " + KEY_REMAINING_DAYS + " = " +KEY_REMAINING_DAYS+"-1";
db.rawQuery(where, null);
}

Aucun commentaire:

Enregistrer un commentaire