0

After lots of research on implementing IntentServices and Alarms together, I've come up with this. I don't know exactly what happens with this code so I need help in knowing exactly what is going on.

public class MainActivity{
  
  //....

    public void onNewItemAdded(String[] _entry){
            
            //...

           Intent intent = new Intent(MainActivity.this, UpdateService.class);
         startService(intent);
    }

  //....
}

public class AlarmReceiver extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {
  // TODO Auto-generated method stub
  Intent startIntent = new Intent(context, UpdateService.class);
  context.startService(startIntent);
      }
    public static final String ACTION_REFRESH_ALARM = "com.a.b.ACTION_REFRESH_ALARM";
}

public class UpdateService extends IntentService{
  //...
  @Override
 public void onCreate() {
  super.onCreate();
  alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  String ALARM_ACTION = AlarmReceiver.ACTION_REFRESH_ALARM;
  Intent intentToFire = new Intent(ALARM_ACTION);
  alarmIntent = PendingIntent.getBroadcast(this, 0, intentToFire, 0);
 }

 @Override
 protected void onHandleIntent(Intent intent) {
  Context context = getApplicationContext();
  SharedPreferences prefs = PreferenceManager
    .getDefaultSharedPreferences(context);
  int updateFreq = Integer.parseInt(prefs.getString(
    PreferencesActivity.PREF_UPDATE_FREQ, "60"));
  boolean autoUpdateChecked = prefs.getBoolean(
    PreferencesActivity.PREF_AUTO_UPDATE, false);
  if (autoUpdateChecked) {
   int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP;
   long timeToRefresh = SystemClock.elapsedRealtime() + updateFreq
     * 60 * 1000;
   alarmManager.setInexactRepeating(alarmType, timeToRefresh,
     updateFreq * 60 * 1000, alarmIntent);
  } 
  else {
   alarmManager.cancel(alarmIntent);
  }
  refreshKeywords();
 }
 }

My aim is to get the refreshKeywords() method to be called every minute. Also, what happens if the onNewItemAdded() method is called more than once?

Sorry if this question is stupid, I'm a beginner.

Bob Bob
  • 154
  • 1
  • 11

1 Answers1

0

If you wish you to call refreshKeywords()method to be called every minutes why do you use AlarmManager like this,

  private void ServiceRunningBackground() {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
    {
        final int restartAlarmInterval = 6000;
        final int resetAlarmTimer = 2*1000;
        final Intent restartIntent = new Intent(this, MyService.class);
        restartIntent.putExtra("ALARM_RESTART_SERVICE_DIED", true);
        final AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Handler restartServiceHandler = new Handler()
        {
            @Override
            public void handleMessage(Message msg) {
                PendingIntent pintent = PendingIntent.getService(getApplicationContext(), 0, restartIntent, 0);
                alarmMgr.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + restartAlarmInterval, pintent);
                sendEmptyMessageDelayed(0, resetAlarmTimer);
            }
        };
        restartServiceHandler.sendEmptyMessageDelayed(0, 0);
    }
}

Just call this method where ever you want and set the time accordingly

JNI_OnLoad
  • 5,472
  • 4
  • 35
  • 60
  • I don't get it. Where does the refreshKeywords() method come in while using this implementation? Is this function to be used as a replacement for the AlarmReceiver class? – Bob Bob Nov 01 '14 at 21:55
  • yes, exactly you can use this function for your AlarmReceiver – JNI_OnLoad Nov 01 '14 at 21:59
  • Ok so how should I modify the onHandleIntent() method? I want the alarm to be set to auto refresh only if the SystemPreferences activity says that it's enabled by the user. I have done that in the onHandleIntent() method. Also, where should this ServiceRunningBackground() method be placed and where should it be called? – Bob Bob Nov 02 '14 at 13:24
  • put ServiceRunningBackground in OnCreate if you want the alarm to be started the movement your app launched otherwise, it is easy you can create any other method and check for this condition using shared pref as I can see and accordingly activate the alarm by calling this method. – JNI_OnLoad Nov 02 '14 at 13:42
  • Ohh ok so basically ServiceRunningBackground() starts an alarm for onHandleIntent() of the UpdateService class. Thank you so so much!! – Bob Bob Nov 02 '14 at 14:33