我是Android新手,我遇到以下問題。我正在編寫一個示例應用程序,我有一個意向服務,首先檢查所有位置提供者以獲取最後一個已知位置。如果最後一個已知位置都沒有提供準確(或及時)足夠的位置,則使用BroadcastReceiver意圖調用位置管理器的requestLocationUpdates方法。每次廣播接收機的onReceive方法被調用時,它都應該檢查位置以確定它是否準確和/或足夠及時。我還有一個意圖服務中的TimerTask,它最終會關閉,並且應該檢查是否已經獲得足夠準確和/或足夠及時的位置更新。我遇到的問題是我不知道如何將廣播接收機中獲取的位置數據返回到意向服務。看起來這應該是一件容易的事情,但我一直在爲此苦苦多日。我能想到的唯一方法就是將數據寫入廣播接收器中的SQLite數據庫,然後將這些記錄讀回到意向服務中,但這似乎不必要的複雜。有誰知道將數據返回到意向服務的正確方法是什麼?我是否應該爲requestLocationUpdates使用廣播接收器?有沒有更簡單的方法來做到這一點?下面是代碼Android開發:從BroadcastReceiver獲取數據通過requestLocationUpdates調用
public class GetLocationService extends IntentService {
public GetLocationService() {
super("something");
}
LocationManager locationManager;
long maxFixLateness;
float maxFixPosUncertainty;
boolean usableLocObtained;
Location bestLoc = null;
float bestLocScore = 0;
Intent locChangeI;
PendingIntent pLocChangeI;
@Override
final protected void onHandleIntent(Intent intent) {
maxFixLateness = 30000;
maxFixPosUncertainty = 30;
long curTime = System.currentTimeMillis();
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
// Check for a usable location fix
List<string> matchingProviders = locationManager.getAllProviders();
for (String provider : matchingProviders) {
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
// ...some code to check if the location is accurate or timely
// enough
}
}
if (bestLoc == null) {
locChangeI = new Intent(this, HandleLocationUpdateReceiver.class);
pLocChangeI = PendingIntent.getBroadcast(this, 0, locChangeI,
PendingIntent.FLAG_UPDATE_CURRENT);
usableLocObtained = false;
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, pLocChangeI);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, pLocChangeI);
// Call the timer that will periodically check to see if a usable
// location has been obtained.
new LocFixCheckTimer(60000, 30, 1000);
}
}
private class LocFixCheckTimer {
Timer timer;
long numChecks;
public LocFixCheckTimer(long initSearchTime, long maxRechecks,
long recheckFreq) {
numChecks = maxRechecks;
timer = new Timer();
// Wait 2 seconds before checking for a fix again
timer.schedule(new CheckLocTask(), initSearchTime, recheckFreq);
}
class CheckLocTask extends TimerTask {
public void run() {
if (numChecks > 0) {
if (usableLocObtained == true) {
// I want to use the location data obtained from the
// HandleLocationUpdateReceiver's onReceive method
// but I don't how to get that data here.
}
} else {
// Cancel the timer. We've timed-out on searching
// for a usable location fix
timer.cancel();
}
--numChecks;
}
}
}
}
這裏是廣播接收器:
public class HandleLocationUpdateReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Location loc = (Location) intent.getExtras().get(LocationManager.KEY_LOCATION_CHANGED);
if (loc != null)
{
double lat = loc.getLatitude();
double lon = loc.getLongitude();
// Do some checking to see how accurate and timely the location is
// here and somehow get it back to the intent service.
}
}
}
感謝您的幫助!
我現在工作了!謝謝您的幫助! – user1057118