我正试图解决一个小问题:
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 120000, 0, mLocationListener);
所以我假设在调用onLocationChanged方法之前,mLocationListener应该等待2个mainutes。但是,每次我将geo fix更新发送到模拟器后,都会立即调用该方法。我是否误解了Android开发人员指南,我是否必须使用计时器或类似的东西来组织我需要的更新速率?
答案 0 :(得分:1)
我认为您对更新的工作方式感到困惑。您无法告诉GPS硬件在将来的特定时间向您发送更新。您只能在需要更新时提供指导。使用minTime,你说你不想每2分钟更频繁地更新一次,但是当你设置你的监听器时你会使用最小距离为零。这告诉底层GPS驱动程序每当位置改变任何数量的距离时都会向您发送更新。这就是您在发送新点时立即获得更新的原因。
答案 1 :(得分:1)
经过一些研究和冥想:)我想出了类似的东西:一种可以在两种模式下运行的服务(取决于listenPeriod变量):1)简单地处理一次坐标并随后关闭自我(listenPeriod) = 0); 2)并以指定的速率(listenPeriod> 0)处理坐标,直到服务关闭。
GPSTracer.listenPeriod = 120000;
comp = new ComponentName(context.getPackageName(), GPSTracer.class.getName());
GPSTracer.iGPSTracer = new Intent(context, GPSTracer.class.getClass());
GPSTracer.iGPSTracer.setComponent(comp);
service=context.startService(GPSTracer.iGPSTracer);
所以我必须在开始服务之前初始化listenPeriod变量。服务部分如下所示:
public class GPSTracer extends Service {
public static Intent iGPSTracer;
public static volatile Location mLocation = null;
public static volatile LocationListener mLocationListener = null;
public static LocationManager mLocationManager = null;
public static long listenPeriod = 0;
Timer mTimer = null;
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.arg1 == 1)
{
if(GPSTracer.mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
GPSTracer.mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 120000, 100, GPSTracer.mLocationListener);
}
else if(GPSTracer.mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
{
GPSTracer.mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 120000, 100, GPSTracer.mLocationListener);
}
else
{
List<NameValuePair> gpsData = new ArrayList<NameValuePair>();
gpsData.add(new BasicNameValuePair("Error", "No location provider"));
stopSelf();
}
}
else if(msg.arg1 == 0)
{
GPSTracer.mLocationManager.removeUpdates(GPSTracer.mLocationListener);
}
};
};
TimerTask selfStopTask = new TimerTask() {
@Override
public void run() {
stopSelf();
}
};
TimerTask mTimerTask = new TimerTask() {
@Override
public void run() {
//Message mMessage = new Message();
//mHandler.sendMessage(mMessage);
if(mLocation == null)
{
mLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(mLocation == null)
{
mLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}
if(mLocation != null)
{
List<NameValuePair> gpsData = new ArrayList<NameValuePair>();
gpsData.add(new BasicNameValuePair("Latitude", (new Double(mLocation.getLatitude())).toString()));
gpsData.add(new BasicNameValuePair("Longitude", (new Double(mLocation.getLongitude())).toString()));
gpsData.add(new BasicNameValuePair("Provider", mLocation.getProvider()));
}
else
{
List<NameValuePair> gpsData = new ArrayList<NameValuePair>();
gpsData.add(new BasicNameValuePair("Error", "Location is unknown"));
}
}
};
@Override
public void onCreate() {
mLocationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
mLocationListener = new GPSListener(getApplicationContext());
};
@Override
public void onStart(Intent intent, int startId) {
Message mMessage = new Message();
mMessage.arg1 = 1;
mHandler.sendMessage(mMessage);
if(listenPeriod == 0)
{
mTimer = new Timer("GPSTask");
mTimer.schedule(mTimerTask, 0);
mTimer.schedule(selfStopTask, 60000);
}
else if(listenPeriod>0)
{
mTimer = new Timer("GPSTask");
mTimer.schedule(mTimerTask, 0, listenPeriod);
}
};
@Override
public void onDestroy() {
Message mMessage = new Message();
mMessage.arg1 = 0;
mHandler.sendMessage(mMessage);
//GPSTracer.mLocationManager.removeUpdates(GPSTracer.mLocationListener);
iGPSTracer = null;
mTimer.cancel();
mTimerTask.cancel();
mTimer = null;
mTimerTask = null;
mLocationListener = null;
mLocationManager = null;
};
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
和LocationListener的实现:
public class GPSListener implements LocationListener{
Context context;
public GPSListener(Context appContext) {
context = appContext;
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
//GPSTracer.mLocationManager.removeUpdates(GPSTracer.mLocationListener);
GPSTracer.mLocation = location;
}
}
我希望它会帮助某人...) 附:感谢Dave MacLean和EboMike!并随意提问;)