Search code examples
androidandroid-intentandroid-servicealarmmanager

How do I change the time interval when the execution of the service it is already included?


Need to change the interval through which the service is running, that is the time at which the service is already running wake up and starts to run the service itself. I can not do because after 1 activation service in my service class ServiseExample more misses to somehow stop it and start knit with the new parameters. How to do it? Here is my main class:

public class ServiceExample extends Service {
    public static Cursor c;
    public static int INTERVAL =1000;
    // 60 min = 3600000
    public static final int FIRST_RUN = 5; // 5 seconds
    int REQUEST_CODE = 11223344;
    private RepeatingAlarmService mSMSreceiver;
    private IntentFilter mIntentFilter;
    static AlarmManager alarmManager;

    @Override
    public void onCreate() {
        super.onCreate();
        mSMSreceiver = new RepeatingAlarmService();
        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
        registerReceiver(mSMSreceiver, mIntentFilter);
        startService(INTERVAL);
        Log.v(this.getClass().getName(), "onCreate(..)");
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.v(this.getClass().getName(), "onBind(..)");
        return null;
    }

    @Override
    public void onDestroy() {
        unregisterReceiver(mSMSreceiver);
        if (alarmManager != null) {
            Intent intent = new Intent(this, RepeatingAlarmService.class);
            alarmManager.cancel(PendingIntent.getBroadcast(this, REQUEST_CODE, intent, 0));
        }
        Toast.makeText(this, "Service Stopped!", Toast.LENGTH_LONG).show();
        Log.v(this.getClass().getName(), "Service onDestroy(). Stop AlarmManager at " + new
                java.sql.Timestamp(System.currentTimeMillis()).toString());
    }

    void startService(int interval) {
        Intent intent = new Intent(this, RepeatingAlarmService.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, REQUEST_CODE, intent, 0);
        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(
                AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + FIRST_RUN,
                interval,
                pendingIntent);
        Toast.makeText(this, "Service Started.", Toast.LENGTH_LONG).show();
        Log.v(this.getClass().getName(), "AlarmManger started at " + new

                java.sql.Timestamp(System.currentTimeMillis()).toString());

    }
}

Here is the class that gets in my service after a certain period of time:

public class RepeatingAlarmService extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
        NetworkInfo nInfo = connMgr.getActiveNetworkInfo();
        android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if(( (wifi.isAvailable() || mobile.isAvailable()) && nInfo != null && nInfo.isConnected()  ))
        {
            Toast.makeText(context,"Error Send" + e,Toast.LENGTH_LONG).show();
        }
    } else  {

        //   CHANGE INTERVAL HERE
        Toast.makeText(context,"Connection False or interval is not good",Toast.LENGTH_LONG).show();
    }
}

Sorry for my English.


Solution

  • To change the repeat interval, just execute this code again with the new interval:

        Intent intent = new Intent(this, RepeatingAlarmService.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, REQUEST_CODE, intent, 0);
        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(
                AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + interval,
                interval,
                pendingIntent);
    

    When you call setRepeating(), if there is already an alarm set with a matching Intent, that alarm will be canceled before the new alarm is set.