2

I'm trying to test the internet connection of my app, and I'm testing it with "ConnectivityManager".

I'would like to call the ConnectivityManager class not from the main class, but from a class a made just to handel the connectivity test.

the method "getSystemService" in the second class gives me an error, how come???

This is the main class calling the connectivitymanager class:

   public void onClick(View v){
        Log.d("mytag", "in onclick");
   CheckInternetpermission obCheckInternetPermission= new CheckInternetpermission();
        Log.d("mytag", "starting permission");
        obCheckInternetPermission.check();
    }

This is the connectivitymanager, "getSystemService" gives me this error: Cannot resolve method 'getSystemService(java.lang.String)

package com.example.network;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.view.View;


public class CheckInternetpermission{
    public void check(){
        Log.d("mytag", "In check");
        ConnectivityManager connMgr = (ConnectivityManager)


getSystemService(Context.CONNECTIVITY_SERVICE); //<---getSystemService give me error??????
            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isConnected()) {
                Log.d("mytag", "Internet connection TRUE");
            } else {
                Log.d("mytag","Internet connection FALSE");    // display error
            }
    }
}
Carlo Luther
  • 2,402
  • 7
  • 46
  • 75

1 Answers1

5

getSystemService() is a method on Context. You need to have a Context to obtain a system service. Your Activity, for example, is a Context, as the Activity class inherits from Context.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • should the check() method be defined as check(Context context)?? how can I solve it? Or better how can I pass a context to the getSystemService() method? – Carlo Luther Jun 18 '13 at 21:12