context.getSystemService() 是一个昂贵的电话吗?
Is context.getSystemService() an expensive call?
context.getSystemService()
通话费用高吗?
即我已经构建了一个小的 http 网络库(我知道还有其他可用的 http 网络库),它使用 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
检查(在执行 http 请求之前)用户是否已连接到互联网(一种快速失败策略) .
我的问题是我应该将 ConnectivityManager
保存为我的 http 库的实例变量(class 字段)还是应该在每次开始 http 请求之前调用 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
检索 "new" ConnectivityManager?每次调用 getSystemService(Context.CONNECTIVITY_SERVICE)
时是否返回相同的 ConnectivityManager 实例(换句话说,将 ConnectivityManger 存储到 class 字段中会导致问题,因为我的 http 库是一个长寿的 --> 长寿作为申请 运行)
My question is should I save the ConnectivityManager as an instance variable (class field) of my http library or should I call ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); every time before I start an http request to retrieve a "new" ConnectivityManager?
我会保留实例。虽然 getSystemService()
实际上调用起来并不昂贵,但为什么调用它比需要的更频繁?
in other words, can storing a ConnectivityManger into a class field lead to problems since my http library is a long living one --> lives as long as application run
为了安全起见,在 Application
单例 (getApplicationContext()
) 上调用 getSystemService()
。 通常,getSystemService()
返回的对象对创建它的Context
一无所知。偶尔,它确实如此 — Android 5.0 中的 CameraManager
遭受此缺陷,尽管它已在 Android 5.1 中修复。如果系统服务对象的寿命将超过我所在的上下文,出于偏执,我倾向于使用 getApplicationContext()
来检索系统服务。
(内存泄漏,他们要抓我了!)
Is the same ConnectivityManager instance returned every time I call getSystemService(Context.CONNECTIVITY_SERVICE)
说实话,我没看过
context.getSystemService()
通话费用高吗?
即我已经构建了一个小的 http 网络库(我知道还有其他可用的 http 网络库),它使用 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
检查(在执行 http 请求之前)用户是否已连接到互联网(一种快速失败策略) .
我的问题是我应该将 ConnectivityManager
保存为我的 http 库的实例变量(class 字段)还是应该在每次开始 http 请求之前调用 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
检索 "new" ConnectivityManager?每次调用 getSystemService(Context.CONNECTIVITY_SERVICE)
时是否返回相同的 ConnectivityManager 实例(换句话说,将 ConnectivityManger 存储到 class 字段中会导致问题,因为我的 http 库是一个长寿的 --> 长寿作为申请 运行)
My question is should I save the ConnectivityManager as an instance variable (class field) of my http library or should I call ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); every time before I start an http request to retrieve a "new" ConnectivityManager?
我会保留实例。虽然 getSystemService()
实际上调用起来并不昂贵,但为什么调用它比需要的更频繁?
in other words, can storing a ConnectivityManger into a class field lead to problems since my http library is a long living one --> lives as long as application run
为了安全起见,在 Application
单例 (getApplicationContext()
) 上调用 getSystemService()
。 通常,getSystemService()
返回的对象对创建它的Context
一无所知。偶尔,它确实如此 — Android 5.0 中的 CameraManager
遭受此缺陷,尽管它已在 Android 5.1 中修复。如果系统服务对象的寿命将超过我所在的上下文,出于偏执,我倾向于使用 getApplicationContext()
来检索系统服务。
(内存泄漏,他们要抓我了!)
Is the same ConnectivityManager instance returned every time I call getSystemService(Context.CONNECTIVITY_SERVICE)
说实话,我没看过