簡體   English   中英

具有locationListener的服務不會更改onLocationChange上的回調的位置

[英]Service with locationListener doesn't change location with callback on onLocationChange

我的服務類遇到問題,當位置更改時,我的位置存儲在ArrayList中。

public class PhoneGpsStateService extends Service {

Context applicationContext;
Boolean isGPSEnabled;
Timer timer;
Boolean isNetworkEnabled;
Long distanceUpdateRange;
Long minimumTimeDistanceUpdate;
List<PositionInfo> positionInfos =  new ArrayList<PositionInfo>();
LocationManager locationManager;
SharedPreferences etasPreference;

public PhoneGpsStateService() {
    super();
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    this.applicationContext = this;
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
    locationManager.getBestProvider(criteria, false);
    etasPreference = PreferenceManager.getDefaultSharedPreferences(applicationContext);
}

@Override
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);
    getCurrentLocation();
}

public void getCurrentLocation() {
    try {
        distanceUpdateRange = Long.decode(etasPreference.getString(getString(R.string.key_distance_settings), null));
        minimumTimeDistanceUpdate = Long.decode(etasPreference.getString(getString(R.string.key_minimum_time_settings), null));
        Log.i("debug", distanceUpdateRange.toString());
        Log.i("debug", distanceUpdateRange.toString());
        //locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);

        // Controlla se il gps del device è attivato
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        // Controlla se la localizzazione con la rete è attivata
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            Log.i("debug", "Gps disattivato e rete disattivata, localizzazione non disponibile");
            Toast.makeText(this, "Gps disattivato e rete disattivata", Toast.LENGTH_LONG).show();
        } else {
            if (isNetworkEnabled) {
                Toast.makeText(this, "Rete abilitata", Toast.LENGTH_LONG).show();
                Log.d("debug", "Rete attivata");
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, minimumTimeDistanceUpdate,
                        distanceUpdateRange, locationListenerNetwork);
                timer=new Timer();
                timer.schedule(new GetLastLocation(), minimumTimeDistanceUpdate);
            }
            if (isGPSEnabled) {
                Toast.makeText(this, "Gps abilitato", Toast.LENGTH_LONG).show();
                Log.d("debug", "GPS Attivato");
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minimumTimeDistanceUpdate,
                            distanceUpdateRange, locationListenerGps);
                    timer=new Timer();
                    timer.schedule(new GetLastLocation(), minimumTimeDistanceUpdate);

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

LocationResult locationResult = new LocationResult(){
    @Override
    public void gotLocation(Location location){
        if (location !=null){
        PositionInfo positionInfo = new PositionInfo();
        positionInfo.setLongitude(location.getLongitude());
        positionInfo.setLatitude(location.getLatitude());
        positionInfo.setPositionDate(new Date(location.getTime()));
        positionInfos.add(positionInfo);
        Log.i("debug", "Posizione salvata");
        for (PositionInfo positionInfo1 : positionInfos) {
            Log.i("debug", positionInfo1.getLatitude()+" "+positionInfo1.getLongitude()+" "+positionInfo1.getPositionDate());
            }
        }
        else
            Log.i("debug", "Posizione non salvata");
    }
};

class GetLastLocation extends TimerTask {
    @Override
    public void run() {
         Log.i("debug", "TimerTask");
         locationManager.removeUpdates(locationListenerGps);
         locationManager.removeUpdates(locationListenerNetwork);

         Location locationNetwork=null;
         Location locationGps=null;

         if(isNetworkEnabled){
                locationNetwork=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
         }
         if(isGPSEnabled){
                locationGps=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
         }

         //torna l'ultima posizione in ordine di tempo
         if(locationGps!=null && locationNetwork!=null){
             if(locationGps.getTime()>locationNetwork.getTime())
                 locationResult.gotLocation(locationGps);
             else
                 locationResult.gotLocation(locationNetwork);
             return;
         }

         if(locationGps!=null){
             locationResult.gotLocation(locationGps);
             return;
         }
         if(locationNetwork!=null){
             locationResult.gotLocation(locationNetwork);
             return;
         }
         locationResult.gotLocation(null);
    }
}

LocationListener locationListenerGps = new LocationListener() {
    public void onLocationChanged(Location location) {
        //Log.v("Verbose", "IN ON LOCATION CHANGE, lat=" + location.getLatitude() + ", lon=" + location.getLongitude());
        timer.cancel();
        locationResult.gotLocation(location);
        locationManager.removeUpdates(this);
        locationManager.removeUpdates(locationListenerNetwork);
    }
    public void onProviderDisabled(String provider) {Log.i("debug", "onProviderDisabled");}
    public void onProviderEnabled(String provider) {Log.i("debug", "onProviderEnabled");}
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.i("debug", "onStatusChanged");
    }
};

LocationListener locationListenerNetwork = new LocationListener() {
    public void onLocationChanged(Location location) {
        timer.cancel();
        locationResult.gotLocation(location);
        locationManager.removeUpdates(this);
        locationManager.removeUpdates(locationListenerGps);
    }
    public void onProviderDisabled(String provider) {Log.i("debug", "onProviderDisabled");}
    public void onProviderEnabled(String provider) { Log.i("debug", "onProviderEnabled");}
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.i("debug", "onStatusChanged"); 
    }
};
public static abstract class LocationResult{
    public abstract void gotLocation(Location location);
}

}

我已將我的locationManager設置為對於很多位置,將minimumTime和minumumDistance設置為0來檢索位置,但是當我移動設備時他不起作用。

我的Android清單:

   <uses-permission android:name="android.permission.ACCESS_GPS" />
   <uses-permission android:name="android.permission.ACCESS_LOCATION" />
   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
   <uses-permission android:name="android.permission.INTERNET" />

提前致謝!

我已經解決了,我已經用onStartCommand替換了重寫方法onStart。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM