How to check if mobile network is enabled/disabled

前端 未结 5 1964
别跟我提以往
别跟我提以往 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:04

    apparently there is an alternative, more clean solution then the Reflection approach:

    final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    final NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    int type = networkInfo.getType();
    String typeName = networkInfo.getTypeName();
    boolean connected = networkInfo.isConnected()
    

    networkInfo.getType() will return '0' when connected to mobile network or '1' when connected trough WIFI. networkInfo.getTypeName() will return the strings "mobile" or "WIFI". and networkInfo.isConnected() will tell you whether or not you have an active connection.

提交回复
热议问题