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