Issue
What will be the context for CONNECTIVITY_SERVICE in Fragmet, i have checked getActivity also but it is giving error.
public boolean isOnline() {
ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
try {
if (connectionManager.getActiveNetworkInfo().isConnected()) {
Log.d("ConStatus", "Data Connection On");
return true;
} else {
Log.d("ConStatus", "Data Connection off");
return false;
}
} catch (NullPointerException e) {
Log.i("ConStatus", "No Active Connection");
return false;
}
}
Solution
getSystemService() is a method on Context. A Fragment would call it using getActivity():
getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
Answered By - CommonsWare
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.