Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.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 locationListener的服务不会使用onLocationChange上的回调更改位置_Java_Android_Android Location - Fatal编程技术网

Java locationListener的服务不会使用onLocationChange上的回调更改位置

Java locationListener的服务不会使用onLocationChange上的回调更改位置,java,android,android-location,Java,Android,Android Location,我的服务类中有一个问题,当位置发生更改时,它将我的位置存储在ArrayList中 public class PhoneGpsStateService extends Service { Context applicationContext; Boolean isGPSEnabled; Timer timer; Boolean isNetworkEnabled; Long distanceUpdateRange; Long minimumTimeDistanceUpdate; List<P

我的服务类中有一个问题,当位置发生更改时,它将我的位置存储在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设置为retrive the position,其中minimumTime和MinumDistance为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