如何以编程方式检测受限移动数据?

时间:2017-11-20 12:32:43

标签: android connection mobile-data

在某些设备(设备可能是也可能不是设备)中,我们可以选择限制wifi和移动数据。
如何以编程方式检查设备受限的互联网连接?

1 个答案:

答案 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应该报告为空。