Fragment中CONNECTIVITY_SERVICE的上下文是什么?

时间:2012-10-25 21:01:57

标签: android

Fragment中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;
            }
        }

1 个答案:

答案 0 :(得分:3)

getSystemService()Context上的一种方法。 Fragment会使用getActivity()调用它:

getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);