带有 locationListener 的服务不会通过 onLocationChange 上的回调更改位置
Service with locationListener doesn't change location with callback on onLocationChange
我的服务有问题 class 当位置发生变化时,谁将我的位置存储在 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。
我的服务有问题 class 当位置发生变化时,谁将我的位置存储在 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。