Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to write a background service in android to check if device is connected to the internet or not?
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/3141807/android-service-to-check-internet-connectivity">Android service to check internet connectivity?</a> </p> </blockquote> <p>I am designing an Android app to track the total data usage (download+upload). The app does the following tasks :-</p> <ol> <li>Continuously checks if the device is connected to the internet or not.</li> <li>If the device is connected to the internet then start counting the uploaded and downloaded data.</li> <li>The count is stored in database and displayed to the user for the current session.</li> <li>When the app is installed the service should be started.</li> <li>When the device is rebooted, the service should be started.</li> </ol> <p>I have tried the following code but us this sufficient?</p> <pre><code>public final boolean isInternetOn() { ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); // ARE WE CONNECTED TO THE NET if( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED || connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING || connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING || connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) { // MESSAGE TO SCREEN FOR TESTING (IF REQ) //Toast.makeText(this, connectionType + †connectedâ€, Toast.LENGTH_SHORT).show(); return true; } else { if( connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED || connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED ) { //System.out.println(“Not Connectedâ€); return false; } return false; } } </code></pre> <p>ANY HELP WOULD BE GREATLY APPRECIATED!</p> <p>-Vaibhav.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload