Android getSystemService()

Android getSystemService(),第1张

概述使用getSystemService()获取系统服务,需要注意的是,总是使用上下文context.getSystemService来调用该方法,如获取网络状态的服务ConnectivityManagermanager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);Statemobile=manager.getNe

使用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()所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1119175.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存