Is the request for internet persmission required at runtime (Android)?

前端 未结 4 787
清歌不尽
清歌不尽 2021-01-18 02:46

For Android, it is required that we ask permissions at runtime to make sure users understand better why en when permissions are needed. I know this is true for permissions l

4条回答
  •  臣服心动
    2021-01-18 03:49

    
    
    
    Your permission is right but you have to check internet connectivity before using any internet related function . You can check internet connected or not by following function 
    
    
    public static boolean isNetworkOnline(Context con)
        {
            boolean status = false;
            try 
            {
                ConnectivityManager cm = (ConnectivityManager) con
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo netInfo = cm.getNetworkInfo(0);
    
                if (netInfo != null && netInfo.getState() == State.CONNECTED) {
                    status = true;
                } else {
                    netInfo = cm.getNetworkInfo(1);
    
                    if (netInfo != null && netInfo.getState() == State.CONNECTED) {
                        status = true;
                    } else {
                        status = false;
                    }
                }
    
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
    
            return status;
        }
    

提交回复
热议问题