Fragmet 中 CONNECTIVITY_SERVICE 的上下文是什么,我也检查了 getActivity 但它给出了错误。
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;
}
}