• Android getSystemService()


    使用getSystemService()获取系统服务,需要注意的是,总是使用上下文

    context.getSystemService

    来调用该方法,如获取网络状态的服务

    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    State mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
    State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    
    // 如果3G、WIFI、2G等网络状态是连接的,则退出,否则显示提示信息进入网络设置界面
    if (mobile == State.CONNECTED || mobile == State.CONNECTING)
      return true;
    if (wifi == State.CONNECTED || wifi == State.CONNECTING)
      return true;

    文档解释:

    Note: System services obtained via this API may be closely associated with the Context in which they are obtained from. 
    In general, do not share the service objects between various different contexts (Activities, Applications, Services, Providers, etc.)

      

  • 相关阅读:
    104. 二叉树的最大深度
    1120. 子树的最大平均值
    1121. 将数组分成几个递增序列
    1118. 一月有多少天
    1110. 删点成林
    102. 二叉树的层次遍历
    145. 二叉树的后序遍历
    94. 二叉树的中序遍历
    144. 二叉树的前序遍历
    剑指offer-0x04
  • 原文地址:https://www.cnblogs.com/xl0715/p/3438109.html
Copyright © 2020-2023  润新知