我有一段相当简单的代码,它是一个定期运行的服务,记录当前位置(使用网络提供商),将其发送到服务器,然后再回到睡眠状态。
我正在两台不同的手机上测试 - 一个定期月度计划的SGS2和带预付费SIM卡的廉价中兴(有数据,但分钟是10c /分钟)。我发现当我拿两部手机并开车时,SGS2工作得很好,但中兴似乎失去了修复的能力。
中兴通讯醒来,设置听众,获取位置修复,但位置指向我的房子(它有最后一个基于wifi的修复),而不是真正的当前位置。该位置的时间戳是最新的,所以当我收到位置更新时,我真的无法判断该位置是否有效(如在SGS2中,或当中兴通讯在家时)或下铺(例如我与中兴通讯一起开车。
之前有没有人有类似的问题?它与预付卡或中兴手机本身有什么关系吗?不幸的是,我不能交换SIM卡(我必须root /解锁手机),所以我无法测试它。
我已经包含了下面的代码,但由于它在SGS2上工作正常,我认为没有太多问题。
public class LocationRecorder extends Service {
private volatile Location lastLocation;
private LocationManager locationManager;
private LocationListener locationListener;
private static volatile PowerManager.WakeLock wakeLock = null;
private static synchronized PowerManager.WakeLock getWakeLock(Context context) {
if (wakeLock == null) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "LocationRecorder");
wakeLock.acquire();
}
return wakeLock;
}
public static void startLocationRecorder(Context context, Intent service) {
getWakeLock(context);
context.startService(service);
}
@Override
public void onCreate() {
Log.d("LocationRecorder", "Starting Location Service");
locationManager = ((LocationManager)getSystemService(LOCATION_SERVICE));
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.d("Location Changed", location.toString());
if (location.getExtras()!=null) {
String x = "";
for (String key : location.getExtras().keySet()) {
x+=key+":"+location.getExtras().get(key).toString()+", ";
}
Log.d("Location Changed Extras", x);
}
setLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Status Changed", provider+" "+status);
if (extras!=null) {
String x = "";
for (String key : extras.keySet()) {
x+=key+":"+extras.get(key).toString()+", ";
}
Log.d("Status Changed Extras", x);
}
}
public void onProviderEnabled(String provider) {
Log.d("Provider Enabled", provider);
}
public void onProviderDisabled(String provider) {
Log.d("Provider Disabled", provider);
}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
waitForLocation();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_REDELIVER_INTENT;
}
@Override
public void onDestroy() {
locationManager.removeUpdates(locationListener);
try {
wakeLock.release();
wakeLock = null;
}
catch (Exception e) {
wakeLock = null;
}
Log.d("LocationRecorder", "Destroying service");
super.onDestroy();
}
protected void waitForLocation() {
new Thread() {
@Override
public void run() {
setLocation(null);
for (int i=0; i<3;i++) {
Log.d("LocationRecorder", "Waiting for location");
try {Thread.sleep(10000);} catch(Exception e) {};
if (getLocation() != null) {
Log.d("LocationRecorder", "Sending new location!");
new Utilities(LocationRecorder.this).updateLocation(getLocation().getLatitude(),
getLocation().getLongitude(), getLocation().getAccuracy());
break;
}
}
stopSelf();
}
}.start();
}
public synchronized void setLocation(Location newLocation) {
lastLocation = newLocation;
}
public synchronized Location getLocation() {
return lastLocation;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}