在某些设备(设备可能是也可能不是设备)中,我们可以选择限制wifi和移动数据。
如何以编程方式检查设备受限的互联网连接?
答案 0 :(得分:-2)
我的工具类:
/**
* Check if the device is connected to the internet.
*
* @param context the application context
* @return boolean if device is online
*/
public static boolean isOnline(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
当您不被允许在后台工作或不允许连接到互联网时,
ConnectivityManager
应该报告为空。