user3671271
user3671271

Reputation: 568

How to use context in a function?

I'm very beginner in android , i write this class for check the network connection:

public class ConnectivityDetector {
    public static boolean IS_INTERNET_AVAILABLE(Context context){
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null)
        {
            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;
    }

}


and with this code i want call that class:

public class AlarmReciever extends BroadcastReceiver {
    Context context;
    @Override
    public void onReceive(Context context, Intent intent) {
 if (ConnectivityDetector.IS_INTERNET_AVAILABLE(AlarmReciever.this)) {
       //write my code
}

    }
}


but in this line:

if (ConnectivityDetector.IS_INTERNET_AVAILABLE(AlarmReciever.this))


i get this error:
enter image description here


how can i solve that?

Upvotes: 3

Views: 56

Answers (1)

Blackbelt
Blackbelt

Reputation: 157487

AlarmReciever is not context, so you can't use this. You have the parameter Context context in on receive. You could use that one.

public class AlarmReciever extends BroadcastReceiver {

     @Override
     public void onReceive(Context context, Intent intent) {
     if (ConnectivityDetector.IS_INTERNET_AVAILABLE(context)) {
            //write my code

Upvotes: 3

Related Questions