我创建了一个在后台运行的服务,可以在手机数据库中保存gps点。
我已经使用广播监听器创建服务到开关屏幕,因为当屏幕关闭时我想每5分钟保存一次点,当屏幕打开时我想每分钟保存一次。
昨天我在手机上试了一下应用程序,并在10小时内节省了超过12000点!!!!
经过深入研究后,我发现相同的点被保存在更多的副本中,从0到250以上,并且点保存的频率(没有副本)是每36秒减少一次...... < / p>
以下是服务的代码:
public class MonitorGpsService extends Service {
private String sharedPreferences= "Settings";
private String viaggio_in_corso = "viaggio_in_corso";
private long id;
private boolean firstStart = true;
private int minTime = 300000; //(5 minuti)
private static String startScan = "dateStart";
private GpsListener gpsListener;
private BroadcastReceiver mReceiver;
private Travel travel;
@Override
public void onCreate() {
id = this.getSharedPreferences(sharedPreferences, MODE_PRIVATE).getLong(viaggio_in_corso, -1);
travel = new Travel(this);
travel.load(id);
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
gpsListener = new GpsListener(travel);
gpsListener.startListener(minTime,0);
this.getSharedPreferences(sharedPreferences, MODE_PRIVATE).edit().putLong(startScan, DateManipulation.getCurrentTimeMs()).commit();
this.getSharedPreferences(sharedPreferences, MODE_PRIVATE).edit().putBoolean("service", true).commit();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
this.getSharedPreferences(sharedPreferences, MODE_PRIVATE).edit().putBoolean("service", false).commit();
gpsListener.stopListener();
unregisterReceiver(mReceiver);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if ((flags & START_FLAG_RETRY) == 0) {
// TODO If it’s a restart, do something.
}
else {
// TODO Alternative background process.
}
boolean screenOn;
try {
screenOn = intent.getBooleanExtra("screen_state", false);
} catch (NullPointerException e) { screenOn = false; }
if ((!screenOn) && (!firstStart)) {
gpsListener.stopListener();
gpsListener.startListener(60000, 0);
this.getSharedPreferences(sharedPreferences, MODE_PRIVATE).edit().putLong(startScan, DateManipulation.getCurrentTimeMs()).commit();
}
else if (!firstStart) {
gpsListener.startListener(minTime, 0);
MediaScan mediaScan = new MediaScan(travel);
try {
mediaScan.scanImages();
} catch (URISyntaxException e) {e.printStackTrace();}
}
firstStart = false;
return Service.START_STICKY;
}
以下是gpsListener的代码:
public class GpsListener {
private LocationManager locationManager;
private String bestProvider;
private LocationListener myLocationListener = null;
private String serviceString = Context.LOCATION_SERVICE;
private Travel travel;
public GpsListener(Travel travel) {
locationManager = (LocationManager)travel.getContext().getSystemService(serviceString);
bestProvider = locationManager.getBestProvider(setCriteria(), true);
this.travel=travel;
}
public void stopListener() {
locationManager.removeUpdates(myLocationListener);
}
public void startListener(int time, int space) {
myLocationListener = new LocationListener() {
Location location1 = null;
public void onLocationChanged(Location location) {
if ((location != null) && (location != location1)) {
try {
savePosition(location);
location1 = location;
} catch (SQLException e) { e.printStackTrace(); }
}
}
public void onProviderDisabled(String provider){
// Update application if provider disabled.
}
public void onProviderEnabled(String provider){
// Update application if provider enabled.
}
public void onStatusChanged(String provider, int status, Bundle extras){
// Update application if provider hardware status changed.
}
};
locationManager.requestLocationUpdates(bestProvider, time, space, myLocationListener);
}
// salva la posizione passatagli nel db
private void savePosition(Location location) throws SQLException {
Points point = new Points();
point.setLatitude((int) (location.getLatitude() * 1E6));
point.setLongitude((int) (location.getLongitude() * 1E6));
point.setDataRilevamento(DateManipulation.getCurrentTimeMs());
travel.addPoints(point);
}
// criteri globali per la gestione del gps
private Criteria setCriteria() {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(false);
return criteria;
}
}
这里是ScreenReceiver的代码
public class ScreenReceiver extends BroadcastReceiver {
private boolean screenOff;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
}
Intent i = new Intent(context, MonitorGpsService.class);
i.putExtra("screen_state", screenOff);
context.startService(i);
}
}
感谢您的帮助。
答案 0 :(得分:2)
是的,这是正常的我认为我做了一个应用程序,每100米和20秒保存位置结果是我在Android 1.6上的100米和20秒之后有5-8分
只需检查距离最后一个点的距离,并将其与最小距离变量进行比较,如果它大于保存它
Android api中有一个距离计算功能。
编辑: 它的 float distanceTo(Location dest)函数的位置实例