2
我在我的IntentService中使用LocationManager時遇到問題。我正在開發一個應用程序來顯示地圖圖像。我想要一個IntentService運行背景來檢測位置更改。當檢測到位置更新時,服務會將新位置寫入SQLite數據庫。 IntentService將發送一個廣播給主Activity,讓它知道一個新的位置已被存儲。主應用程序提取數據庫的位置並將圖標移動到地圖上的新位置。在IntentService中使用LocationManager似乎不起作用
下面是服務代碼:
import java.util.List;
import java.io.*;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.GpsStatus.NmeaListener;
import android.location.LocationManager;
import android.database.*;
import android.util.Log;
// local imports
import com.saic.MCAppActivity;
import com.saic.db.*;
public class SaService extends IntentService {
private static final String tag = "SaService";
private Location loc = null;
private Intent updateMapIntent = null;
private LocationManager locMgr = null;
private boolean gps_provider = false;
private boolean net_provider = false;
public SaService()
{
super(tag);
}
public SaService(String name)
{
super(name);
}
@Override
protected void onHandleIntent(Intent intent)
{
updateMapIntent = new Intent();
updateMapIntent.setAction("UPDATE_LOCATION");
locMgr = MCAppActivity.getLocationManager();
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60, 0,
locationListener);
locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60, 0,
locationListener);
// loop until told to quit
while (!MCAppActivity.toQuit())
{
try {
Thread.sleep(30000);
}
catch (InterruptedException e) {}
getBaseContext().sendBroadcast(updateMapIntent);
}
stopSelf();
}
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
double lat = 0.0;
double lon = 0.0;
// Called when a new location is found by the network location provider.
if (location.getProvider().equals(LocationManager.GPS_PROVIDER))
{
lat = location.getLatitude();
lon = location.getLongitude();
Log.i(tag, "LocationListener(): gps_provider location update:\nlat: " + lat +
"\nlon: " + lon);
writeLocationToDB(location);
}
else if (location.getProvider().equals(LocationManager.NETWORK_PROVIDER))
{
lat = location.getLatitude();
lon = location.getLongitude();
Log.i(tag, "LocationListener(): net_provider location update:\nlat: " + lat +
"\nlon: " + lon);
writeLocationToDB(location);
}
else
{
Log.i(tag, "LocationListener(): unknown provider found");
}
}
// don't need them now. make them NO-OP
public void onStatusChanged (String provider, int status, Bundle extras) {}
public void onProviderEnabled (String provider)
{
Log.i(tag, "enable gps: "+ provider);
}
public void onProviderDisabled (String provider)
{
Log.w(tag, "disable gps: "+ provider);
}
};
}
中的LocationManager在我MCAppActivity定義,並傳遞給服務。在手機上運行應用程序時,我沒有遇到任何錯誤。但是,偵聽器從不被調用。在服務中調用LocationManager與調用Activity相比是否存在問題?我知道我必須將minTime更改爲更大的值以避免電池耗盡。現在,我只想看看我能否實現它。
在此先感謝。
我不會使用'IntentService',因爲你是戰鬥吧 - '了Thread.sleep()'在生產代碼中是一個嚴重的代碼味道。看看我的'LocationPoller'爲另一個模型:https://github.com/commonsguy/cwac-locpoll – CommonsWare
IntentService抑制LocationManager的功能嗎?我沒有讀到任何關於這是一個問題。 (它可能,但看起來很奇怪,javadoc頁面沒有提到它。) –
它更多的是我認爲你阻塞了'onLocationUpdate()'應該被調用的線程。 – CommonsWare