使用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.)
转载于:https://www.cnblogs.com/xl0715/p/3438109.HTML
总结以上是内存溢出为你收集整理的Android getSystemService()全部内容,希望文章能够帮你解决Android getSystemService()所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)