Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过编程检查Android上是否启用了“使用无线网络”?_Java_Android_Connection_Wifi - Fatal编程技术网

Java 如何通过编程检查Android上是否启用了“使用无线网络”?

Java 如何通过编程检查Android上是否启用了“使用无线网络”?,java,android,connection,wifi,Java,Android,Connection,Wifi,我不想检查WIFI或3G上是否有任何连接! 我只是想验证,而不是改变!如果启用了“使用无线网络”复选框,则可以在“设置”>“位置和安全”>“我的位置”>“此处”中设置该复选框 Thanx使用此代码 public Boolean isNetAvailable(Context con) { try { ConnectivityManager connectivityManager = (ConnectivityManager) con.getSystemService(Conte

我不想检查WIFI或3G上是否有任何连接! 我只是想验证,而不是改变!如果启用了“使用无线网络”复选框,则可以在“设置”>“位置和安全”>“我的位置”>“此处”中设置该复选框 Thanx使用此代码

public Boolean isNetAvailable(Context con) {

   try {
      ConnectivityManager connectivityManager = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

      if(wifiInfo.isConnected()) {
         return true;
      }
   }catch(Exception e){
      e.printStackTrace();
   }
   return false;
}
此操作需要以下权限

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> 
使用此代码

public Boolean isNetAvailable(Context con) {

   try {
      ConnectivityManager connectivityManager = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

      if(wifiInfo.isConnected()) {
         return true;
      }
   }catch(Exception e){
      e.printStackTrace();
   }
   return false;
}
此操作需要以下权限

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> 

来自RajaReddy的代码将检查网络是否启用了Wifi。如果您想知道是否为定位启用了Wifi,那么应该使用有用的内容替换上下文

ContentResolver cr = context.getContentResolver();
boolean wifiEnabled = Settings.Secure.isLocationProviderEnabled(cr, LocationManager.NETWORK_PROVIDER);
API级别<8的示例代码

private static boolean isWifiLocationEnabled (Context context) {
    ContentResolver cr = context.getContentResolver();
    String enabledProviders = Settings.Secure.getString(cr, Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if (!TextUtils.isEmpty(enabledProviders)) {
        // not the fastest way to do that :)
        String[] providersList = TextUtils.split(enabledProviders, ",");
        for (String provider : providersList) {
            if (LocationManager.NETWORK_PROVIDER.equals(provider)) {
                return true;
            }
        }
    }
    return false;
}

来自RajaReddy的代码将检查网络是否启用了Wifi。如果您想知道是否为定位启用了Wifi,那么应该使用有用的内容替换上下文

ContentResolver cr = context.getContentResolver();
boolean wifiEnabled = Settings.Secure.isLocationProviderEnabled(cr, LocationManager.NETWORK_PROVIDER);
API级别<8的示例代码

private static boolean isWifiLocationEnabled (Context context) {
    ContentResolver cr = context.getContentResolver();
    String enabledProviders = Settings.Secure.getString(cr, Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if (!TextUtils.isEmpty(enabledProviders)) {
        // not the fastest way to do that :)
        String[] providersList = TextUtils.split(enabledProviders, ",");
        for (String provider : providersList) {
            if (LocationManager.NETWORK_PROVIDER.equals(provider)) {
                return true;
            }
        }
    }
    return false;
}

我没有在Settings.Secure中启用isLocationProviderEnabled方法。我能以某种方式访问它吗?好的,该方法是API级别8及以上。String allowedProviders=Settings.Secure.getStringcr,Settings.Secure.LOCATION\u PROVIDERS\u ALLOWED;应该为您提供一个逗号分隔的已启用提供程序列表,您可以在其中查找LocationManager.NETWORK\u Provider就是这样!当启用“使用gps卫星”和“使用无线网络”两个选项时,列表包含网络、gps。你让我开心!我没有在Settings.Secure中启用isLocationProviderEnabled方法。我能以某种方式访问它吗?好的,该方法是API级别8及以上。String allowedProviders=Settings.Secure.getStringcr,Settings.Secure.LOCATION\u PROVIDERS\u ALLOWED;应该为您提供一个逗号分隔的已启用提供程序列表,您可以在其中查找LocationManager.NETWORK\u Provider就是这样!当启用“使用gps卫星”和“使用无线网络”两个选项时,列表包含网络、gps。你让我开心!这很好,但如果用户在android设置中选中了使用无线网络,我仍然无法找到这种方法。上面的代码只是检查是否有WIFI/网络连接…这不是我想要的。sdfThis工作正常,但如果用户在android设置中选中了“使用无线网络”,我仍然无法找到这种方法。上面的代码只是检查是否有WIFI/网络连接…这不是我想要的。自卫队