Android 配置请求位置更新的通知频率

Android 配置请求位置更新的通知频率,android,Android,我正在尝试解决一个小问题: mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 120000, 0, mLocationListener); 所以我假设mLocationListener在调用其onLocationChanged方法之前应该等待2次mainutes。然而,每次我向emulator发送geo-fix更新后,就会调用该方法。我是否误解了android开发者指南,我是否必须使用计时器或任何类似工具

我正在尝试解决一个小问题:

mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 120000, 0, mLocationListener);

所以我假设mLocationListener在调用其onLocationChanged方法之前应该等待2次mainutes。然而,每次我向emulator发送geo-fix更新后,就会调用该方法。我是否误解了android开发者指南,我是否必须使用计时器或任何类似工具来组织我需要的更新速率?

我想你对更新的工作原理感到困惑。您不能告诉GPS硬件在将来的特定时间向您发送更新。您只能就您希望更新的时间向it部门提供指导。在minTime中,您表示不希望更新频率超过每2分钟一次,但在设置侦听器时,您使用的最小距离为零。这会告诉底层GPS驱动程序在位置改变距离时向您发送更新。这就是为什么当你发送一个新的点时,你会立即得到更新。

经过一些研究和思考:)我想出了这样一个方法:一个可以在两种模式下运行的服务(取决于listenPeriod变量):1)只需处理坐标一次,然后关闭self(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;
}
}

我希望它能帮助某人……)
另外,感谢戴夫·麦克林和迈克!;并且可以自由提问;)

来自docs:minTime-通知的最小时间间隔,以毫秒为单位。此字段仅用作节能提示,位置更新之间的实际时间可能大于或小于此值。soooo。。。基本上,我必须实现一些额外的机制来组织更新的周期性(基于minTime值)?不是真的。你在真正的设备上试过吗?嗯。。。我想我知道了。。。这就是为什么我决定使用timer和timertask来组织我的位置变量的实际更新。嘿,我找到了一篇关于所有这些GPS参数的好文章:希望它能帮助那些寻找答案的人
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;
    }