Checking internet connection with service on android

S.M_Emamian

I know how to check for internet connectivity when my app is open using activity. But how to check for connectivity in service when my app is not running?

Adnan

You might need to use broadcast receiver. You will continuously receive updates in connectivity.(Connected/Disconnected)

Example:

Manifest:

Permissions:

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Register broadcast receiver:

<receiver android:name=".ConnectivityChangeReceiver" >
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>

Create receiver class:

public class ConnectivityChangeReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {

        // Explicitly specify that which service class will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),
                YourService.class.getName());
        intent.putExtra("isNetworkConnected",isConnected(context));
        startService(context, (intent.setComponent(comp)));
    }

 public  boolean isConnected(Context context) {
           ConnectivityManager connectivityManager = ((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE));
           NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
           return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
   }

}

Your service class:

class YourService extends IntentService{

    @Override
    protected void onHandleIntent(Intent intent) {
      Bundle extras = intent.getExtras();
      boolean isNetworkConnected = extras.getBoolean("isNetworkConnected");
      // your code

   }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android:Checking for Internet Connection

From Dev

Android:Checking for Internet Connection

From Dev

checking internet connection on android programming

From Dev

Android - Checking internet connection before starting an intent

From Java

Broadcast receiver for checking internet connection in android app

From Dev

android checking internet connection when a button is pressed

From Dev

android - Service need internet connection

From Dev

checking Internet connection with HttpClient

From Dev

Checking internet connection with Python

From Dev

Checking Internet connection in WebView

From Dev

checking Internet connection with HttpClient

From Dev

Android Intent Service that pauses when no internet connection

From Dev

C# checking Internet connection

From Dev

Cordova - Checking WIFI connection to internet

From Dev

Checking internet connection using Task

From Dev

Checking if the internet connection is lost at runtime

From Dev

Checking internet connection using Task

From Dev

Checking for internet connection or wifi connection with reachabilitySwift

From Dev

Checking a good connection in Android

From Dev

What is wrong with this method of checking for an internet connection?

From Dev

How to perform AsyncTask for checking internet connection

From Dev

Windows Phone 7 checking for Internet Connection

From Dev

How to perform AsyncTask for checking internet connection

From Dev

checking the internet connection and display the result in the TextView class

From Dev

Android Volley - Checking internet state

From Dev

Checking for internet connectivity in Android app

From Dev

android internet connection avalability

From Dev

Android internet connection issue

From Dev

Checking Internet Connection without URL test connection using java

Related Related

  1. 1

    Android:Checking for Internet Connection

  2. 2

    Android:Checking for Internet Connection

  3. 3

    checking internet connection on android programming

  4. 4

    Android - Checking internet connection before starting an intent

  5. 5

    Broadcast receiver for checking internet connection in android app

  6. 6

    android checking internet connection when a button is pressed

  7. 7

    android - Service need internet connection

  8. 8

    checking Internet connection with HttpClient

  9. 9

    Checking internet connection with Python

  10. 10

    Checking Internet connection in WebView

  11. 11

    checking Internet connection with HttpClient

  12. 12

    Android Intent Service that pauses when no internet connection

  13. 13

    C# checking Internet connection

  14. 14

    Cordova - Checking WIFI connection to internet

  15. 15

    Checking internet connection using Task

  16. 16

    Checking if the internet connection is lost at runtime

  17. 17

    Checking internet connection using Task

  18. 18

    Checking for internet connection or wifi connection with reachabilitySwift

  19. 19

    Checking a good connection in Android

  20. 20

    What is wrong with this method of checking for an internet connection?

  21. 21

    How to perform AsyncTask for checking internet connection

  22. 22

    Windows Phone 7 checking for Internet Connection

  23. 23

    How to perform AsyncTask for checking internet connection

  24. 24

    checking the internet connection and display the result in the TextView class

  25. 25

    Android Volley - Checking internet state

  26. 26

    Checking for internet connectivity in Android app

  27. 27

    android internet connection avalability

  28. 28

    Android internet connection issue

  29. 29

    Checking Internet Connection without URL test connection using java

HotTag

Archive