使用asynctask的GPS跟踪器示例:错误“提供程序不存在:null”

时间:2013-03-09 16:15:35

标签: android

我正在寻找一个非常示例的例子,用asynctask显示经度和纬度跟踪器。

我找到了这段代码https://stackoverflow.com/a/6788457,但他没有工作:/我抓住了这个奇怪的例外情况"提供商不存在:null"在:

public boolean startService() {
    try {
        // this.locatorService= new
        // Intent(FastMainActivity.this,LocatorService.class);
        // startService(this.locatorService);

        FetchCordinates fetchCordinates = new FetchCordinates();
        fetchCordinates.execute();
        return true;
    } catch (Exception error) {
        Log.i("exception", error.getMessage());
        return false;
    }

}

搜索完成后,我看到这篇文章(https://stackoverflow.com/a/13851305/2137454)解释说我必须使用这一行:

List<String> providers = locationManager.getAllProviders();

但是我不知道在我的gpstracker课程中使用这些技巧的地点和方式......有人可以帮助我吗?

这里是完整的gpstracker类:​​

public class GPSTracker extends Service implements LocationListener {

    private final Context mContext;
    boolean isGPSEnabled = false;
    boolean isNetworkEnabled = false;
    boolean canGetLocation = false;

    Location location = null; 
    double latitude = 0; 
    double longitude = 0; 
    double altitude = 0;

    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
    private static final long MIN_TIME_BW_UPDATES =  1000* 60 * 1;
    protected LocationManager locationManager;

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

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

            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
               Log.i("Je tombe...","...ici !");
            } else {
                this.canGetLocation = true;
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                                altitude = location.getAltitude();
                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }

    public void stopUsingGPS() {
        if (locationManager != null) {
            Log.i("STOPUSINGGPS", "EFFECTIF");
            locationManager.removeUpdates(GPSTracker.this);
        }
    }

    public double getLatitude() {
        if (location != null) {
            Log.i("LATITUDE", "EFFECTIF");
            latitude = location.getLatitude();
        }
        return latitude;
    }

    public double getLongitude() {
        if (location != null) {
            Log.i("LONGITUDE", "EFFECTIF");
            longitude = location.getLongitude();
        }
        return longitude;
    }

    public double getAltitude() {
        if (location != null) {
            Log.i("ALTITUDE", "EFFECTIF");
            altitude = location.getAltitude();
        }
        return altitude;
    }

    public boolean canGetLocation() {
        return this.canGetLocation;
    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

}

编辑:我的gps已开启

0 个答案:

没有答案