Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8516535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:27:23+00:00 2026-06-11T05:27:23+00:00

I have a service which periodically downloads some updates from a web site. Default

  • 0

I have a service which periodically downloads some updates from a web site.
Default period is long, about 10 mins and I do that with an AlarmManager which starts this service.

Network can be on or off, in case it’s off service should download news as soon as network returns on, because interval is long and, in the worst case, you could have to wait 20 min till next update.

I made a network state broadcast receiver, and, with little code I made a NetworkListener interface:

public interface NetworkStateListener {
public void onNetworkOn();
public void onNetworkDown();
}

So my service has these 2 methods and can know when network goes on or off.

My idea is the following: in the onStart() method I spawn a new thread: if network is off thread waits, otherwise downloads. When onNetworkOn() is called I call a notify():

private boolean threadWaiting; //a flag so that I don't spawn multiple threads
public int onStartCommand(Intent intent, int flags, int startId) {
    if(!threadWaiting){//if already a thread waiting do nothing and exit
        Thread t = new Thread(new Runnable(){

            @Override
            public void run() {
                synchronized(binder){
                    while(!isNetworkOn())
                        try {
                            threadWaiting = true;
                            binder.wait();
                        }catch(InterruptedException ex){
                            ex.printStackTrace();
                        }
                }
                // blocks until network connection is available
               threadWaiting = false;
               //... operations ...

And this is onNetworkOn() method:

public void onNetworkOn() {
    synchronized(binder){
        binder.notify();
    }

is this the right way to proceed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:27:25+00:00Added an answer on June 11, 2026 at 5:27 am

    Solved:
    I keep a boolean flag “missed update in my service”:

    boolean missedUpdate = false;
    

    then in my onStartCommand:

    if(isNetworkOn()){
        //do actual code
    }else
        missedUpdate = true;
    

    while in my on network on:

    public void onNetworkOn(){
        if(missedUpdate){
           //download updated news
           // reset timer
           missedUpdate = false;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a little confused about Accept-Encoding . I have Web Service which would
i have a windows service written in .net 4 which doing jobs periodically using
I have web service which i want to use to upload image to the
I have web service which return json string like : d={main0ID:abc.es/main,main1ID:ah/main} I wanna append
I have web-service which give list of property at a particular area My problem
I have a service running which periodically checks a folder for a file and
I have a function save() which will send some data to server periodically after
I have a Service downloading data every second from a web server (This Service
I have a Windows service which I want to periodically execute an external program.
I have a service which creates a notification and then updates it with certain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.