Keeping a GPS service alive and optimizing battery

2020-02-24 04:14发布

I must build an application with a GPS tracker running during about a day. I'm aware of similar questions in SO but I haven't found any answers to some questions I have.

-I need a GPS fix every 10 min, so I think the best way to do it is to start the location service, get a fix (or timeout) and stop the service (with removeUpdates()). How can I have an application (or service or whatever) running this cycle every 10min and be sure it will continue as long as there is some battery left (even if device goes to sleep, it should wake it up every 10min to get a fix)? Is using AlarmManager a good idea?

-Can I expect the battery to last one day with this method?

I've checked mytracks but the gps listener seems always on and the battery is expected to last no more than 5h.

I've also checked CWAC Location Poller but it does only removeUpdates() on timeout and restart the listener immediately. It also uses a wakelock while in my case I think an AlarmManager could be a better idea.

Any help/suggestion welcome

标签: android gps
2条回答
冷血范
2楼-- · 2020-02-24 04:19

You are spot on with alarm manager I use

    Intent serviceIntent = new Intent(this, TrackerService.class);
    mPendingIntent = PendingIntent.getService(this, 0, serviceIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    alarmManager.cancel(mPendingIntent);
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis(), interval, mPendingIntent);

in a similar app for getting network location

interval is ms between starting the service

the service starts up, gets the location and closes

this was MUCH more battery efficient that hanging around with an active service waiting for reports

that code i posted cancels the previous alarms first so you don't get more than 1 :)

查看更多
叛逆
3楼-- · 2020-02-24 04:26

You can check out Passive Receiver It'll give you an location update whenever another the devices location gets updated - only works on 2.2 or later

查看更多
登录 后发表回答