感謝CommsWare。由於評論欄中的空間限制,我使用這種方式來回應。
用戶應該拍下他們即將拾取的物品的照片並將其發送到服務器。完成此操作後,服務器會發回一個輪詢週期,以便電話定期發送其位置。我已經設置了textViews
來顯示獲得的經緯度(見下文),但是這個部分直到兩晚前纔開始工作。在我稍微有點耐心並且讓手機花時間獲得職位後,它終於有了工作。我認爲問題在於我不允許手機獲得定位,所以它無法正常工作。現在的問題是,定期更新可能無法工作,根據您所說的內容,並基於事實,即在我獲得定位之前,我在完全晴朗的天空下站在外面約2分鐘。
下面是一些代碼的快照(這部分是在主要活動的onCreate覆蓋拿到第一定位):
`LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, new LocationListener() {
public void onStatusChanged(String provider, int status,
Bundle extras) {
// called when the provider status changes. Possible
// status: OUT_OF_SERVICE, TEMPORARILY_UNAVAILABLE or
// AVAILABLE.
}
public void onProviderEnabled(String provider) {
// called when the provider is enabled by the user
}
public void onProviderDisabled(String provider) {
// called when the provider is disabled by the user, if
// it's already disabled, it's called immediately after
// requestLocationUpdates
}
public void onLocationChanged(Location location) {
double latitute = location.getLatitude();
double longitude = location.getLongitude();
tv5.setText(""+latitute);
tv6.setText(""+longitude);
}
});
`
下面是SOAP請求的一部分,響應處理機制。
`//在幾秒鐘內獲得投票時間。 String [] getPollNumber; getPollNumber = resultParams [2] .split(「;」);
//Set up the intent to be passed to the tracking service.
Intent updateLocation = new Intent (this,TrackUser.class);
PendingIntent pendingIntent = PendingIntent.getService(PickUp.this, 0, updateLocation, 0);
//Set up the alarm manager to be used to periodically send updates.
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//If instruction = 1, start tracking the user.
if(instruction == 1)
{
//Start tracking service on phone.
pollPeriod = Integer.parseInt(getPollNumber[0]);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (pollPeriod*1000), pollPeriod*1000, pendingIntent);
}
else
{
if(instruction == 0)
{
//Stop tracking service.
stopService(new Intent(this,TrackUser.class));
alarmManager.cancel(pendingIntent);
}
}
'
在跟蹤服務類,我用的是從它在onStart()重寫,簡單地打包當前座標到SOAP對象,並更新服務器調用的方法。我擔心如果手機像你說的那樣睡覺,GPS在這些更新期間可能沒有足夠的時間鎖定。我想知道服務何時開始,我應該有一個計時器(可能是requestLocationUpdates的minTime設置?)讓手機保持清醒狀態約3分鐘,以便修復可以進來嗎?請讓我知道這些代碼片段是否有幫助,如果可以更新位置的想法,謝謝。