【Android】SecurityException

 
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"/>

 

 

posted @ 2012-09-12 23:59  Hao Renjie  阅读(18038)  评论(0编辑  收藏  举报