.isProviderEnabled(LocationManager.NETWORK_PROVIDER)在Android中始终为true [英] .isProviderEnabled(LocationManager.NETWORK_PROVIDER) is always true in Android

查看:563
本文介绍了.isProviderEnabled(LocationManager.NETWORK_PROVIDER)在Android中始终为true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么,但是我的varaiable isNetowrkEnabled 总是返回true。无论我的设备上的互联网是否启用都没关系。



这是我的 GPSTracker 类:


private final Context mContext;

布尔isNetworkEnabled = false;
布尔值canGetLocation = false;

位置位置; // location

保护LocationManager locationManager;

公共GPSTracker(上下文环境){
this.mContext = context;
getLocation();


public Location getLocation(){
locationManager =(LocationManager)mContext
.getSystemService(LOCATION_SERVICE);

isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if(!isGPSEnabled&&!isNetworkEnabled){
//没有启用网络提供程序
} else {
this.canGetLocation = true;
if(isNetworkEnabled){
System.out.println(Network enabled);
} else {
System.out.println(Network disabled);



$ / code $ / pre

你知道可能是什么吗?在这段代码中出错?

解决方案

代码中没有任何错误,很可能。提供者是否已启用取决于设置应用程序的位置部分(或设备制造商在其他位置提供的等效控件,例如在应用程序小部件中)。只要网络提供者在设置中未被禁用, isProviderEnabled(LocationManager.NETWORK_PROVIDER)将返回 true 。由于缺乏网络连接, 提供者 与提供者是否工作无关。


I don't know why, but my varaiable isNetowrkEnabled always return true. It doesn't matter if internet on my device is enabled or no.

This is my GPSTracker class:

public class GPSTracker extends Service implements LocationListener{

        private final Context mContext;

        boolean isNetworkEnabled = false;    
        boolean canGetLocation = false;

        Location location; // location

        protected LocationManager locationManager;

        public GPSTracker(Context context) {
            this.mContext = context;
            getLocation();
        }

    public Location getLocation() {
            locationManager = (LocationManager) mContext
                    .getSystemService(LOCATION_SERVICE);

            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                if (isNetworkEnabled) {
                    System.out.println("Network enabled");
                } else {
                    System.out.println("Network disabled");
                }
            }   
    }

Do you know what may be wrong in this code?

解决方案

There is nothing wrong in your code, in all likelihood. Whether a provider is enabled is determined by the Location portion of the Settings app (or equivalent controls offered elsewhere by the device manufacturer, such as in an app widget). So long as the network provider is not disabled in Settings, isProviderEnabled(LocationManager.NETWORK_PROVIDER) will return true. The provider being enabled has nothing to do with whether the provider will work given your lack of network connection.

这篇关于.isProviderEnabled(LocationManager.NETWORK_PROVIDER)在Android中始终为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆