我在 android 程序中使用下面的代码来修改网络连接。
ConnectivityManager conMgr =
(ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
如果是在 Activity 中,代码可以正常运行,但是如果在一个通用类中,就不能调用方法。
getSystemService
如何解决这个问题?
我在 android 程序中使用下面的代码来修改网络连接。
ConnectivityManager conMgr =
(ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
如果是在 Activity 中,代码可以正常运行,但是如果在一个通用类中,就不能调用方法。
getSystemService
如何解决这个问题?
需要传递一个Context对象过去,例如NetUtils
外部调用NetUtils.isConnected(Context context);
内部实现
public static boolean isConnected(Context context) {
ConnectivityManager conMgr =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
//do next
}