检查互联网是否开启

时间:2016-07-02 13:20:42

标签: android

public boolean isCon()
{
    boolean ison;
    ConnectivityManager connMgr = (ConnectivityManager) getActivity()
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isConnected()) {
        // fetch data
        return true;
    } else {
        // display error
        return false;
    }

}
     super.onActivityCreated(savedInstanceState);

        boolean check=isCon();


        Button btn = (Button) getActivity().findViewById(R.id.pnrButton);
        final EditText ed = (EditText) getActivity().findViewById(R.id.pnrEditText);
}

显示此错误

07-02 18:46:53.511 1191-1191 / com.example.jitesh.rcot1 E / AndroidRuntime:FATAL EXCEPTION:main

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jitesh.rcot1/com.example.jitesh.rcot1.MainActivity}: java.lang.SecurityException: ConnectivityService: Neither user 10040 nor current process has android.permission.ACCESS_NETWORK_STATE.
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
                                                                        at android.app.ActivityThread.access$600(ActivityThread.java:122)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                        at android.os.Looper.loop(Looper.java:137)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:4340)
                                                                        at java.lang.reflect.Method.invokeNative(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:511)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
                                                                        at dalvik.system.NativeStart.main(Native Method)
                                                                     Caused by: java.lang.SecurityException: ConnectivityService: Neither user 10040 nor current process has android.permission.ACCESS_NETWORK_STATE.
                                                                        at android.os.Parcel.readException(Parcel.java:1327)
                                                                        at android.os.Parcel.readException(Parcel.java:1281)
                                                                        at android.net.IConnectivityManager$Stub$Proxy.getActiveNetworkInfo(IConnectivityManager.java:597)
                                                                        at android.net.ConnectivityManager.getActiveNetworkInfo(ConnectivityManager.java:365)
                                                                        at com.example.jitesh.rcot1.MyFragment.isCon(MyFragment.java:62)
                                                                        at com.example.jitesh.rcot1.MyFragment.onActivityCreated(MyFragment.java:88)
                                                                        at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1983)
                                                                        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1092)
                                                                        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
                                                                        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
                                                                        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                                        at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:339)
                                                                        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:601)
                                                                        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
                                                                        at android.app.Activity.performStart(Activity.java:4475)

2 个答案:

答案 0 :(得分:0)

Try this:
    in manifeast:uses-permission 
     <android:name="android.permission.INTERNET"/>
     <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

       public static boolean isNetworkStatusAvialable (Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager != null) 
    {
        NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
        if(netInfos != null)
            if(netInfos.isConnected()) 
                if (netInfos.isAvailable())
                    return true;
    }
    return false;

}
   if (isNetworkStatusAvialable(getApplicationContext())) {
 Toast.makeText(getApplicationContext(), "Internet Connection is available", Toast.LENGTH_SHORT).show();
}else
 Toast.makeText(getApplicationContext(), "Turn on Internet Connection", Toast.LENGTH_SHORT).show();
{

答案 1 :(得分:0)

public static boolean isInternetConnected(Context context) {

    ConnectivityManager connectivityManager
            = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

这是必需的许可