无法在requestLocationUpdates()方法中更改时间?

时间:2014-01-27 07:23:16

标签: android google-maps geolocation

在这里,我希望每隔5分钟向服务器发送经度和经度更新,但在我的代码中,我在requestLocationUpdates()方法中更改了最小时间,但它没有改变。我的应用程序每隔1分钟发送更新是固定的。看下面的代码

 @Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    //TODO do something useful
    //Toast.makeText(getBaseContext(), "Got in!!!", Toast.LENGTH_SHORT).show();
    System.out.println("Got in");

     Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();        

        //---use the LocationManager class to obtain locations data---
        lm = (LocationManager)
                getSystemService(Context.LOCATION_SERVICE);


        Intent i = new Intent(this, MyBroadcastReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(
                this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

      //---request for location updates using GPS---
        lm.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER,
                2000,
                0,
                pendingIntent);     
        lm.requestLocationUpdates( 
                LocationManager.GPS_PROVIDER, 
                2000, 
                0, 
                pendingIntent);

    return START_STICKY;
  }


@Override
public void onDestroy() {
    //---remove the pending intent---
    lm.removeUpdates(pendingIntent);

    super.onDestroy();
    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();        
}

@Override
public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "onLocationChanged", Toast.LENGTH_LONG).show();   

}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "onProviderDisabled", Toast.LENGTH_LONG).show();   

}

@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "onProviderEnabled", Toast.LENGTH_LONG).show();   

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "onStatusChanged", Toast.LENGTH_LONG).show();   

}

任何人都可以帮助我。谢谢你。

2 个答案:

答案 0 :(得分:0)

如果您想每五分钟发送一次GPS坐标,那么您必须这样写

 lm.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER,
                5 * 60 * 1000,
                0,
                pendingIntent);     
 lm.requestLocationUpdates( 
                LocationManager.GPS_PROVIDER, 
                5 * 60 * 1000, 
                0, 
                pendingIntent);

答案 1 :(得分:0)

如果您想每五分钟发送一次GPS坐标,那么您必须这样写

//---request for location updates using GPS---
            lm.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER,
                    300000,
                    0,
                    pendingIntent);     
            lm.requestLocationUpdates( 
                    LocationManager.GPS_PROVIDER, 
                    300000, 
                    0, 
                    pendingIntent);