Android: GPS location updates when the cellphone is in sleep?

試著忘記壹切 提交于 2019-12-06 10:21:49

问题


I need to update the location through GPS in the background even when the phone is in sleep. I am thinking to use AlarmManager to broadcast an Intent and then a receiver will call requestLocationUpdates() on LocationManager. But I am not sure whether it's guaranteed that the GPS location will be updated when the phone is in sleep and an intent will be broadcasted if I registered a PendingIntent when call requestLocationUpdates()?

thanks,


回答1:


GPS aren't working when screen is off! You may set alarm for you app, but to get GPS position you must make screen on.

Updated at 2016-08-23:

Please be note, this answer is old and this information not actual for last os versions.




回答2:


You could use a service.

A service doesn't have a visual user interface, but rather runs in the background for an indefinite period of time. For example, a service might play background music as the user attends to other matters, or it might fetch data over the network or calculate something and provide the result to activities that need it. Each service extends the Service base class.

If your phone go to sleep the service will continue its work in background. Remember to start the service using startService() instead of bindService() otherwise when the activity that start the service go to sleep the service will be stopped.

hope this helps..



来源:https://stackoverflow.com/questions/3010011/android-gps-location-updates-when-the-cellphone-is-in-sleep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!