摘录自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"/>

 

相关文章:

  • 2021-07-23
  • 2022-12-23
  • 2021-05-20
  • 2021-12-23
  • 2022-12-23
  • 2022-02-12
  • 2022-12-23
猜你喜欢
  • 2021-09-14
  • 2021-05-26
  • 2021-07-20
  • 2022-12-23
  • 2021-09-30
  • 2022-12-23
  • 2021-08-26
相关资源
相似解决方案