Android GPS 节省了太多积分

发布于 2024-11-19 22:21:34 字数 5806 浏览 5 评论 0原文

我创建了一个在后台运行的服务,该服务将 GPS 点保存在手机数据库上。

我还使用开关屏幕的广播侦听器创建了该服务,因为当屏幕关闭时,我想每 5 分钟保存一次点,当屏幕打开时,我想每分钟保存一次。

昨天我在手机上尝试了这个应用程序,10小时内就节省了12000多积分!!!!

深入研究后,我发现同一个点被保存在更多副本中,从0到超过250个,而且点保存的频率(没有副本)是每36秒少或多一个......

这是代码服务的:

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);
    }

}

谢谢您的帮助。

I have created a service running in background which saves gps points on the phone db.

I have created the service using also a broadcast listener to the on-off screen, because when the screen is off I want to save points every 5 minutes, when the screen is on I want to save them every minute.

Yesterday I tried the app on my cell phone, and, in 10h it saved more than 12000 points!!!!

After looked deeply I found out that the same point is saved in more copies, from 0 to over 250, and also that the frequency of point saving (withouth the copies) is one every 36 seconds less or more...

Here is the code of the service:

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;
}

Here is the code of the 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;
    }
}

And here i the code for the 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);
    }

}

Thank you for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦÷微暖 2024-11-26 22:21:35

是的,这很正常,我想我制作了一个应用程序,每 100m 20 秒保存一次位置,结果是在 Android 1.6 上的 100m 20 秒后我有 5-8 个点,

只需检查与最后一个点的距离并进行比较到一个最小距离变量,如果它更大,那么保存它,

Android api中有一个用于距离计算的函数。

编辑:
它的
位置实例的 float distanceTo(Location dest) 函数

yeah this is normal I think Ive made an app which saved the position every 100m and 20 seconds the result was I had 5-8 points after the 100m and 20 seconds that was on Android 1.6

just check the distance from the last point and compare it to a min distance variable if its greater then save it

there is a function for distance calculation in the Android api .

EDIT:
its the
float distanceTo(Location dest) function of a location instance

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文