6

我需要检查Android中是否可以使用互联网,所以我尝试使用以下功能:

boolean b_IsConnect = isNetworkAvailable();
private boolean isNetworkAvailable() {
  ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo activeNetworkInfo = connectivityManager
    .getActiveNetworkInfo();
  return activeNetworkInfo != null;
}

我还在清单中添加了所需的权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

但它仍然抛出异常:

05-02 16:05:26.226: E/AndroidRuntime(481): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.housedisplay/com.housedisplay.RssActivity}: java.lang.SecurityException: ConnectivityService: Neither user 10038 nor current process has android.permission.ACCESS_NETWORK_STATE.
05-02 16:05:26.226: E/AndroidRuntime(481):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
05-02 16:05:26.226: E/AndroidRuntime(481):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
05-02 16:05:26.226: E/AndroidRuntime(481):  at android.app.ActivityThread.access$1500(ActivityThread.java:123)
05-02 16:05:26.226: E/AndroidRuntime(481):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
05-02 16:05:26.226: E/AndroidRuntime(481):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 16:05:26.226: E/AndroidRuntime(481):  at android.os.Looper.loop(Looper.java:126)
05-02 16:05:26.226: E/AndroidRuntime(481):  at android.app.ActivityThread.main(ActivityThread.java:3997)
05-02 16:05:26.226: E/AndroidRuntime(481):  at java.lang.reflect.Method.invokeNative(Native Method)
05-02 16:05:26.226: E/AndroidRuntime(481):  at java.lang.reflect.Method.invoke(Method.java:491)
05-02 16:05:26.226: E/AndroidRuntime(481):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-02 16:05:26.226: E/AndroidRuntime(481):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-02 16:05:26.226: E/AndroidRuntime(481):  at dalvik.system.NativeStart.main(Native Method)
05-02 16:05:26.226: E/AndroidRuntime(481): Caused by: java.lang.SecurityException: ConnectivityService: Neither user 10038 nor current process has android.permission.ACCESS_NETWORK_STATE.
05-02 16:05:26.226: E/AndroidRuntime(481):  at android.os.Parcel.readException(Parcel.java:1322)
05-02 16:05:26.226: E/AndroidRuntime(481):  at android.os.Parcel.readException(Parcel.java:1276)
05-02 16:05:26.226: E/AndroidRuntime(481):  at android.net.IConnectivityManager$Stub$Proxy.getActiveNetworkInfo(IConnectivityManager.java:446)
05-02 16:05:26.226: E/AndroidRuntime(481):  at android.net.ConnectivityManager.getActiveNetworkInfo(ConnectivityManager.java:256)
05-02 16:05:26.226: E/AndroidRuntime(481):  at com.housedisplay.RssActivity.isNetworkAvailable(RssActivity.java:218)
05-02 16:05:26.226: E/AndroidRuntime(481):  at com.housedisplay.RssActivity.onCreate(RssActivity.java:59)
05-02 16:05:26.226: E/AndroidRuntime(481):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
05-02 16:05:26.226: E/AndroidRuntime(481):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)

你能告诉我这里发生了什么吗?

4

6 回答 6

10

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >缺少一个结束斜线。可能会导致解析 XML 的问题,这可能会导致下一个<uses-permission ...>无法正确解析。

于 2012-05-03T11:58:00.440 回答
2

您需要在 Manifest.xml 中的 application-tag 之外设置权限

像这样它应该可以工作:

private boolean connectionChecker() {
    // ConnectivityManager casten
    connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    // boolean für networkinfo holen
    activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    // Returnt true oder false if not null
    return activeNetworkInfo != null;
}
于 2012-05-02T10:43:07.063 回答
1

试试这样

private void checkNetworkState() {
        // TODO Auto-generated method stub
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager
                .getActiveNetworkInfo();

        if (activeNetworkInfo == null) {

            Toast.makeText(this, "Network Not Connected...Please Try Again",
                    Toast.LENGTH_LONG).show();

        } else {
            if (activeNetworkInfo.isConnected()) {

                try {
                    << Your Task>>>

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    System.out.println("Inside the Exception" + e.getMessage());
                }

            } else if (activeNetworkInfo.isConnectedOrConnecting()) {

                Toast.makeText(this,
                        "Network is connecting Now please patient",
                        Toast.LENGTH_LONG).show();
            }

        }
    }

在清单中添加此权限 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />(在应用程序标记上方放置权限标记)

于 2012-05-02T10:50:05.580 回答
1

试试这个代码

public boolean isNetworkOnline() {
boolean status=false;
try{
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getNetworkInfo(0);
    if (netInfo != null && netInfo.getState()==NetworkInfo.State.CONNECTED) {
        status= true;
    }else {
        netInfo = cm.getNetworkInfo(1);
        if(netInfo!=null && netInfo.getState()==NetworkInfo.State.CONNECTED)
            status= true;
    }
}catch(Exception e){
    e.printStackTrace();  
    return false;
}
return status;

}  
于 2012-05-02T10:54:02.093 回答
0
NetworkInfo i = conMgr.getActiveNetworkInfo();   if (i == null)
    return false;   if (!i.isConnected())
    return false;   if (!i.isAvailable())
    return false;   return true;
于 2012-05-02T11:48:09.227 回答
0

您只需要在 Andriod Manifesto 中的应用程序标签上方放置权限

于 2016-03-21T04:50:00.030 回答