[英]Location tracking in background while onChanging of the location in Android
I am developing android application using the Location. 我正在使用Location开发android应用程序。 I am able to get the current location using following code.
我可以使用以下代码获取当前位置。
public void GetLocation()
{
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
private LocationManager mLocationManager;
private String mProvider;
mLocationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(isGPSEnabled && isNetworkEnabled)
{
mProvider = mLocationManager.getBestProvider(criteria, false);
Location location = mLocationManager.getLastKnownLocation(mProvider);
Sring mLatitude=String.valueOf(arg.getLatitude());
String mLongitude=String.valueOf(arg.getLongitude());
}
}
I need to update the location of the user in background, once the location is changed not frequently. 一旦位置更改不频繁,我需要在后台更新用户的位置。 How can I achieve this?
我该如何实现?
You need to use location listener. 您需要使用位置监听器。 You can listen the location in specific meters or specific time interval.
您可以按特定的仪表或特定的时间间隔收听位置。 Check this tutorial.
查看本教程。
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
**MINIMUM_TIME_BETWEEN_UPDATES**,
**MINIMUM_DISTANCE_CHANGE_FOR_UPDATES**,
new MyLocationListener()
);
public class CurrentLatLng implements LocationListener {
public static final int GPS_NOT_ENABLED = -1;
public static final int VALID = 1;
LocationManager manager;
Context context;
public CurrentLatLng(Context context) {
this.context = context;
}
public void getCurrentLatLng() {
//Check if GPS is enabled
if (Commons.isGPSEnabled(context)) {
manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10, 10, this);
} else {
// GPS NOT ENABLED. EVEN THEN THE LOCATION WILL BE RECIEVED AS WE ARE GETTING LOCATION BY NETWORK_PROVIDER
}
}
@Override
public void onLocationChanged(Location l) {
// HERE YOU WILL GET THE LATEST LOCATION AND WILL BE UPDATING WHENEVER YOU CHANGE YOUR LOCATION.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.