是context.getSystemService()
昂贵的电话?context.getSystemService()是一个昂贵的调用吗?
I.e.我建立了一个小的http网络库(我知道还有其他的http网络库可用),它使用ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
来检查(在执行http请求之前)用户是否连接到互联网(某种失败的快速策略)。
我的问题是我应该将ConnectivityManager
保存为我的http库的实例变量(类字段)还是每次在我启动一个http请求以检索“新”ConnectivityManager之前调用ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
?每次我呼叫getSystemService(Context.CONNECTIVITY_SERVICE)
时都会返回相同的ConnectivityManager实例(换句话说,可以将ConnectivityManger存储到类字段中导致问题,因为我的http库是长期活的 - >只要应用程序运行,就会生存)