Search code examples
androidservicealarmmanagerormliteauto-update

Update stored content everyday with Service(AlarmManager) or launch period deduction


I have List of contacts, which I can save on some period. And I should to update the terms of saving everyday. Simply, minus one day to terms of all contacts everyday.

I see two possible solutions :

1) Service, which will use AlarmManager and update content at specified time everyday.

2) Rememeber the last launch of the application, check the difference and deduct this difference from stored terms..

Second approach seems to me more perfomance efficient. Can somebody advise something?


Solution

  • I solved my issue.

    I use method, which is called in onCreate() method in class, which extends from Application class:

    private void setRecurringAlarm(Context context) {
                   Intent intent = new Intent(AlarmReceiver.ACTION_ALARM);
                      AlarmManager alarms = (AlarmManager) this
                             .getSystemService(Context.ALARM_SERVICE);
                   final PendingIntent pIntent = PendingIntent.getBroadcast(this,
                     1234567, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
                   alarms.setRepeating(AlarmManager.RTC_WAKEUP,
                     System.currentTimeMillis(), AlarmManager.INTERVAL_DAY, pIntent);
    
                  Toast.makeText(getApplicationContext(), "started", Toast.LENGTH_SHORT).show();
        }
    

    My AlarmReceiver class:

      public class AlarmReceiver extends BroadcastReceiver {
    
        private static final String DEBUG_TAG = "AlarmReceiver";
        public static String ACTION_ALARM = "com.alarammanager.alaram";
        @Override
        public void onReceive(Context context, Intent intent) {
    
            Intent downloader = new Intent(context, UpdateService.class);
            downloader.setAction(Constants.UPDATE_SERVICE);
            context.startService(downloader);
            Toast.makeText(context, "Entered", Toast.LENGTH_SHORT).show();
        }
    }
    

    and my Service, where desired operation is executed:

          public class UpdateService extends IntentService {
    
            Boolean isRunning = false;
            private ContentRepository _contentRepo;
            public UpdateService() {
                super("UpdateService");
            }
            @Override
            public int onStartCommand(Intent intent, int flags, int startId) {
                faq = new FrequentlyUsedMethods(this);
                _contentRepo = new ContentRepository(this.getContentResolver(), this);
                this.context = this;
                if (!isRunning)
                    archiveDaysDeduct();
    
                return START_REDELIVER_INTENT;
            }
    
        private void archiveDaysDeduct() 
            {
                isRunning = true;
                Log.i("updateService", "archiveDaysDeduct");
                ArrayList<ContactItem> contacts = faq.getDisplayContacts(this);
             Toast.makeText(getApplicationContext(), "update service and I'm here", Toast.LENGTH_LONG).show();
                DatabaseHandler db = new DatabaseHandler(context.getApplicationContext());
                Dao<ContactItem,Integer> daoContact = null;
    
                String yourDate = faq.getCurrentDate();
                if (SharedPrefs.getInstance().getSharedPrefs()!=null)
    
                    if (!SharedPrefs.getInstance().getSharedPrefs().getString(Constants.CURRENT_DATE, "").equalsIgnoreCase(yourDate))
                    {       
    SharedPrefs.getInstance().getSharedPrefs().edit().putString(Constants.CURRENT_DATE, yourDate);
    
                         try {
                             daoContact = db.getContactDao();
                         }
                         catch(Exception e)
                         {
                             e.printStackTrace();
                         }
                         Toast.makeText(getApplicationContext(), "I'm going to decrement the save dates", Toast.LENGTH_SHORT).show();
                        UpdateBuilder<ContactItem, Integer> updateBuilder = daoContact.updateBuilder();
                         DeleteBuilder<ContactItem, Integer> daoContactDelete = daoContact.deleteBuilder();
                         Log.i("contacts size in update service", Integer.toString(contacts.size()));
                        for (ContactItem a : contacts)
                        {
                            if (a.getDays()==0)
                            {
                                try {
    
                                    daoContactDelete.where().eq("id", a.getId());
                                    daoContact.delete(daoContactDelete.prepare());
    
                                } 
                                catch (SQLException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
    
                            }
                            else 
                            {
                                try {
                                    Log.i("contacts to update days", a.toString());
                                    a.setDays(a.getDays()-1);
                                    daoContact.update(a);
                                } catch (SQLException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        }
                        stopSelf();
    
                    }
                }
            }
        }