Display an alert when internet connection not available in android application

后端 未结 13 713
有刺的猬
有刺的猬 2020-12-08 11:48

In my application data comes from internet and I am trying to create a function that checks if a internet connection is available or not and if it isn\'t, it gives an alert

13条回答
  •  失恋的感觉
    2020-12-08 12:13

    public boolean isOnline() 
    {
    
    
    ConnectivityManager connectionManager;
    
    if(app_context!=null)
    
    connectionManager = (ConnectivityManager) app_context.getSystemService(Context.CONNECTIVITY_SERVICE);
    
            else
                return false;
    
            try 
            {
                if (connectionManager.getActiveNetworkInfo().isConnected()) 
                {
                    Log.e(THIS_FILE, "Communicator ....isConnected()");
                    return true;
                } 
                else
                { 
                    Log.e(THIS_FILE, "Communicator ....isNotConnected()");
                    return false;
                }
            } 
            catch (NullPointerException e) 
            {
                Log.e(THIS_FILE, "No Active Connection");
                return false;
            }
        }
    

    set permission in manifest

    
        
    

提交回复
热议问题