Reputation: 568
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))
how can i solve that?
Upvotes: 3
Views: 56
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