6
我正在嘗試做的事情是,當收到c2dm消息時,啓動一個服務,要求提供'x'時間的位置,然後將該位置交給我們的服務器。 c2dm消息正確啓動服務,GPS位置打開,但它不會更新。它只是在那裏指定的時間長度(當前是12秒),並且什麼都不做。我在我的應用程序中的其他地方使用完全相同的代碼(而不是服務),並且它完美地工作。我究竟做錯了什麼?將LocationManager作爲服務啓動Android
這會在接收到c2dm消息時啓動服務。
context.startService(new Intent(context, ServicePingLocation.class));
這是服務本身的代碼。所有被調用的,都是「onCreate」和「onStart」。
public class ServicePingLocation extends Service implements LocationListener {
private final String DEBUG_TAG = "[GPS Ping]";
private boolean xmlSuccessful = false;
private boolean locationTimeExpired = false;
private LocationManager lm;
private double latitude;
private double longitude;
private double accuracy;
@Override
public void onLocationChanged(Location location) {
Log.d(DEBUG_TAG, "onLocationChanged");
latitude = location.getLatitude();
longitude = location.getLongitude();
accuracy = location.getAccuracy();
}
@Override
public void onProviderDisabled(String provider) {
Log.d(DEBUG_TAG, "onProviderDisabled");
Toast.makeText(
getApplicationContext(),
"Attempted to ping your location, and GPS was disabled.",
Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(String provider) {
Log.d(DEBUG_TAG, "onProviderEnabled");
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10f, this);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(DEBUG_TAG, "onStatusChanged");
}
@Override
public void onCreate() {
Log.d(DEBUG_TAG, "onCreate");
}
@Override
public void onDestroy() {
Log.d(DEBUG_TAG, "onDestroy");
}
@Override
public IBinder onBind(Intent intent) {
Log.d(DEBUG_TAG, "onBind");
return null;
}
@Override
public void onStart(Intent intent, int startid) {
Log.d(DEBUG_TAG, "onStart");
lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10f, this);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000,
300f, this);
Log.d(DEBUG_TAG, lm.toString());
new SubmitLocationTask(ServicePingLocation.this).execute();
}
private void locationTimer() {
new Handler().postDelayed(new Runnable() {
// @Override
@Override
public void run() {
locationTimeExpired = true;
}
}, 12000);
}
private class SubmitLocationTask extends AsyncTask<String, Void, Boolean> {
/** application context. */
private Context context;
private Service service;
public SubmitLocationTask(Service service) {
this.service = service;
context = service;
}
@Override
protected void onPreExecute() {
locationTimer(); // Start 12 second timer
}
@Override
protected void onPostExecute(final Boolean success) {
if (success && xmlSuccessful) {
lm.removeUpdates(ServicePingLocation.this);
onDestroy();
} else {
if (!GlobalsUtil.DEBUG_ERROR_MSG.equals(""))
Toast.makeText(getBaseContext(),
GlobalsUtil.DEBUG_ERROR_MSG, Toast.LENGTH_SHORT)
.show();
GlobalsUtil.DEBUG_ERROR_MSG = "";
}
}
@Override
protected Boolean doInBackground(final String... args) {
try {
DateFormat df = null;
df = new SimpleDateFormat("M/d/yy h:mm a");
Date todaysDate = new Date();// get current date time with
// Date()
String currentDateTime = df.format(todaysDate);
while ((accuracy > 100f || accuracy == 0.0)
&& !locationTimeExpired) {
// We just want it to sit here and wait.
}
return xmlSuccessful = SendToServerUtil.submitGPSPing(
0, longitude,
latitude, accuracy, currentDateTime);
} catch (Exception e) {
return false;
}
}
}
}
[編輯] 固定的,我遇到的問題。代碼實際上工作。我添加了網絡提供商,調整了onDestroy()方法以停止服務,並調整了用於獲取GPS信號的時間。
謝謝您的建議,CommonsWare
12秒可能在所有情況下都不夠長。你可以看看我的['LocationPoller'](https://github.com/commonsguy/cwac-locpoll),它是爲這樣的場景而設計的。 – CommonsWare 2011-12-29 23:17:18
嗨瑞安,我實際上正在做類似的事情,我無法讓我的代碼工作。我用你的代碼測試它,它不會編譯。在代碼中,變量「xmlSuccessful」和類「SendToServerUtil」,「GlobalsUtil」是什麼? – 2013-02-12 20:10:42
xmlSuccessful是我在將位置發送到服務器之後用於處理REST響應的布爾值。 SendToServerUtil是我的一個實用程序文件,包含我所有的服務器通信調用(例如「SendLocation」)。 GlobalsUtil只是一個普通的工具類,我將所有的全局變量存儲在其中。最後一個可能不是好習慣,但它是我第一個真正的應用程序之一...你活着,你學習,對吧? 無論哪種方式,你可以刪除所有這些引用,你不應該有任何問題。 – RyanInBinary 2013-02-18 14:24:15