aF.
aF.

Reputation: 66697

How to execute a task every 6h or when net is available for Android?

Well, I want to check the version of a site (this part I know how) every 6h or so.

So, I was thinking about making a service for this and use AlarmManager for it.

Since I need Internet to check the version of the site, I need something to see if the internet is on or to see when it's turned on. After the time passed I'll


So my questions (yep, not just one!) are:

  1. Does AlarmManager works even if the display goes to sleep? When the device wakes up it knows how many time as passed and if passed more that 6h it executes the task?
  2. How to check when internet is available?
  3. How to know when internet is turned on? (some kind of broadcast?)
  4. Is this a good solution?

Upvotes: 0

Views: 239

Answers (2)

Yury
Yury

Reputation: 20936

About the alarm manager. Here is a possible code:

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis() + 10000, 6*60*60*1000, pendingIntent);

The first parameter influence how device behave:

RTC - alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up

RTC_WAKEUP - wake up the device when it goes off

Upvotes: 1

R.daneel.olivaw
R.daneel.olivaw

Reputation: 2679

Alarm Manager:

The alarm manager does not have anything to do with the display state, so Yes it can work even if the screen is off.

Network Avaiability snippet:

public boolean isNetworkAvailable() {
  Context context = getApplicationContext();
  ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  if (connectivity == null) {
     boitealerte(this.getString(R.string.alert),"getSystemService rend null");
  } else {
     NetworkInfo[] info = connectivity.getAllNetworkInfo();
     if (info != null) {
        for (int i = 0; i < info.length; i++) {
           if (info[i].getState() == NetworkInfo.State.CONNECTED) {
              return true;
           }
        }
     }
  }
  return false;
}

This function will return true if the network is available, false if it is not (airplane mode, out of reach, etc.)

Don't forget to add permission in your manifest

A possible solution

Have broadcast receiver for the screen off & screen on events like below,

public class ScreenReceiver extends BroadcastReceiver {

    public static boolean wasScreenOn = true;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            // do whatever you need to do here
            wasScreenOn = false;
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            // and do whatever you need to do here
            wasScreenOn = true;
        }
    }
}

In this receiver give the logic for requesting if network is available..

Upvotes: 3

Related Questions