How to check if mobile network is enabled/disabled

前端 未结 5 1945
别跟我提以往
别跟我提以往 2020-12-09 23:31

I would like to know if the mobile network is enabled or disabled.

My application is designed to help the user when he receives a phone call, and to do this I need I

5条回答
  •  攒了一身酷
    2020-12-10 00:03

    you can use below code this is working for all API versions:

    ConnectivityManager cm =
                (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwork != null &&
                              activeNetwork.isConnectedOrConnecting();
    
    if(isConnected)
    {
    if(activeNetwork.getType()==ConnectivityManager.TYPE_MOBILE)
    return true;    
    
    else
        return false;
    }
    
    else
        return false;
    

提交回复
热议问题