为什么我的 GPS 会“挂断”?
我正在尝试实现一个 GPS 位置侦听器,该侦听器将在后台保持开启并不断更新。我意识到这是不好的做法,但我这样做是为了测试目的。现在我有一个通过 getGPS() 调用的位置侦听器。也可以使用killGPS() 来杀死它。
当我四处走动并单击“获取 GPS”按钮时,它可以完美地工作,该按钮从我获得的位置参数中获取纬度和经度。
GPSActivity.getGPS();
loc_listener = GPSActivity.loc_listener;
locationManager = (LocationManager)
getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0, loc_listener);
location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
但是在后台,有时它会说我仍然在我的公寓,即使我一直在外出3小时! 为什么我的位置侦听器不会自行更新,即使它每 10 分钟就会打开一次以查找新位置?
谢谢
package com.cellphone;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class GPSActivity extends Activity{
public static LocationListener loc_listener = null;
public static void getGPS() {
if (loc_listener == null) {
loc_listener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
};
}
}
public static void killGPS(LocationManager locationManager) {
if (locationManager != null && loc_listener != null) {
locationManager.removeUpdates(loc_listener);
}
}
}
I am trying to implement a gps location listener that will stay on and continuously update in the background. I realize this is bad practice but I am doing this for testing purposes. Right now I have a location listener that gets called with getGPS(). It can also be killed with killGPS().
It works perfectly when im walking around and I click my get gps button which takes the latitude and longitude from my location parameter which I get from
GPSActivity.getGPS();
loc_listener = GPSActivity.loc_listener;
locationManager = (LocationManager)
getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0, loc_listener);
location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
However in the background, sometimes it will say I'm still at my apartment even though I've been out for 3hours!
Why is my location listener not updating itself even though it turns on to find a new location every 10 mins?
Thanks
package com.cellphone;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class GPSActivity extends Activity{
public static LocationListener loc_listener = null;
public static void getGPS() {
if (loc_listener == null) {
loc_listener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
};
}
}
public static void killGPS(LocationManager locationManager) {
if (locationManager != null && loc_listener != null) {
locationManager.removeUpdates(loc_listener);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议仅使用 Activity 来显示信息,并在后台使用持有部分唤醒锁的服务,这样即使设备尝试进入睡眠模式,它也能继续运行。然后在活动中使用广播接收器从服务获取位置更新。
无论您如何管理它,您都需要持有唤醒锁以确保它继续运行。
检查此链接:
http://developer.android.com/reference/android/os/PowerManager.html
您必须持有权限:
对于唤醒锁:
I would recommend only using the Activity to display information and use a service in the background that holds a partial wake lock so it continues to run even if the device tries to go into sleep mode. Then use a broadcastreceiver in the Activity to get updates from the service with the location.
Regardless of how you manage it you need to hold a wake lock to ensure it continues running.
Check this Link:
http://developer.android.com/reference/android/os/PowerManager.html
You must hold permission:
And for the wake lock:
对于位置轮询器,这里很简单&工作示例:
https://github.com/commonsguy/cwac-locpoll
您可以通过以下方式保持应用程序的状态在应用程序上下文中添加函数。也就是说,位置轮询器可以了解应用程序状态,并在应用程序未处于活动状态时跳过 GPS 操作。
for location poller, here is simple & working example:
https://github.com/commonsguy/cwac-locpoll
you can keep state of your app by adding a function in Application context. So to say, location poller can be aware of app-state and skip gps action if app is not active.