检查是否有在线连接

时间:2014-04-10 15:17:46

标签: android

我试图制作一个小函数来查看运行我的应用的Android是否具有在线连接。

我已经尝试过运行它,但我在这一行上收到错误:

NetworkInfo [] info = connectivity.getAllNetworkInfo();

这是我的代码(按钮只是发一条小信息说我是否已连接到互联网)

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       Button check = (Button) findViewById(R.id.buttonCheck);
        check.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            ConnectionDetector Connection = new ConnectionDetector(getApplicationContext());
            boolean hasConnection = Connection.isConnectionToInternet();
            if(hasConnection) {
                Toast.makeText(getApplicationContext(), "Está ligado à internet", Toast.LENGTH_LONG);
            }else {
                Toast.makeText(getApplicationContext(), "Não está ligado à internet", Toast.LENGTH_LONG);
            }
                }
        });

ConnectionDetector Class

package com.example.testlayout.app;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectionDetector {

    private Context context ;

    public ConnectionDetector(Context context){
        this.context = context ;
    }

    public boolean isConnectionToInternet(){
        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 ;
    }

}

logcat的:

04-10 16:15:35.597  26543-26543/com.example.testlayout.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.testlayout.app, PID: 26543
    java.lang.SecurityException: ConnectivityService: Neither user 10117 nor current process has android.permission.ACCESS_NETWORK_STATE.
            at android.os.Parcel.readException(Parcel.java:1465)
            at android.os.Parcel.readException(Parcel.java:1419)
            at android.net.IConnectivityManager$Stub$Proxy.getAllNetworkInfo(IConnectivityManager.java:881)
            at android.net.ConnectivityManager.getAllNetworkInfo(ConnectivityManager.java:620)
            at com.example.testlayout.app.ConnectionDetector.isConnectionToInternet(ConnectionDetector.java:22)
            at com.example.testlayout.app.MainActivity$1.onClick(MainActivity.java:56)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

将此添加到您的清单

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

答案 1 :(得分:0)

由于您没有添加uses-permission来访问网络状态,这就是为什么它会抛出Exception ...

java.lang.SecurityException: ConnectivityService: Neither user 10117 nor current process has android.permission.ACCESS_NETWORK_STATE.

现在,在Manifest文件中添加此权限...

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

如果您想了解wifi网络状态,还可以添加以下权限。

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />