首页 > 代码库 > SecurityException

SecurityException

摘录自http://www.cnblogs.com/haorenjie/archive/2012/09/12/2682655.html

public boolean checkNetwork() {
    boolean result = false;
        
    try {
        Context context = this.getApplicationContext();
        ConnectivityManager connectivityMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityMgr.getActiveNetworkInfo();
        if (networkInfo != null) {
            result = networkInfo.isAvailable();
        }
    }
    catch (Exception e) {
        Log.e("test", "get active network info leave: " + e.getMessage());
    }
        
    return result;
}

 

 简单的网络检查,却在connectivityMgr.getActiveNetworkInfo();时抛出如下异常:

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

原因:SecurityException,显然是权限不够。

解决方案:在AndroidManifest.xml中,加入如下权限:

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

 

SecurityException