我正在制作一个必须在许多活动中使用用户当前位置的应用程序。我真的不想在每个活动中都实现一个位置客户端。在我的应用中使用当前位置的最佳方式是什么?可能是后台服务?如果是这样,我该如何使用它?有什么例子吗?
3 回答
首先,“Hemant Chand Dungriyal”的答案不正确。该类继承自Service,但不使用Service组件提供的任何功能,所以基本上只是一个普通的类,不如去掉“扩展Service”部分!(显然我没有足够的声誉发表评论!)
总的来说,我认为每个客户端最好在 android 中管理他们自己与 LocationProvider 服务的连接,这样他们就可以请求符合他们需要的位置。例如,如果一个活动只需要一个粗略的位置,另一个需要系统的最后一个已知位置,另一个需要一个精细的位置,您不会通过为所有活动提供精细的位置来浪费系统资源。
但是,如果您知道应用程序的所有部分都需要精确的位置,那么使用 Service 是一种选择。但是你应该管理服务的生命周期(无论你希望它是绑定服务,还是启动服务?,何时创建以及何时销毁它?等等)你还需要知道所有活动何时关闭并且它是安全的关闭服务。
另一种方法是在应用程序类中实现代码。您可以在第一个活动需要访问它时进行初始化,然后当所有活动都关闭时,您可以将其关闭(同样,您需要一种机制来确定是否还有需要知道位置的可见活动)。
对于实现示例,我相信您可以找到很多,只需 google 即可。
使用以下代码获取当前位置 lat long:-
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
public class GPSTracker extends Service implements LocationListener
{
private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location; // location
public static double latitude; // latitude
public static double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context context)
{
this.mContext = context;
getLocation();
}
public Location getLocation()
{
try
{
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled)
{
// no network provider is enabled
}
else
{
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled)
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
// Log.d("Network", "Network");
if (locationManager != null)
{
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
// System.out.println("latitude if (isNetworkEnabled) => "+latitude);
// System.out.println("longitude if (isNetworkEnabled) => "+longitude);
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled)
{
if (location == null)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
// Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null)
{
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
// System.out.println("latitude if (isGPSEnabled) => "+latitude);
// System.out.println("longitude if (isGPSEnabled) => "+longitude);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
@Override
public void onLocationChanged(Location location)
{
}
@Override
public void onProviderDisabled(String provider)
{
}
@Override
public void onProviderEnabled(String provider)
{
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
}
您想在下面的代码中获取经纬度调用:-
new GPSTracker(Activity.this);
double latitude = GPSTracker.latitude; // latitude
double longitude = GPSTracker.latitude; // latitude
我建议使用 Google Play 服务遵循此示例,然后从生成的 FragmentActivity 扩展需要位置的活动:http: //developer.android.com/training/location/receive-location-updates.html
如果您在 Play 商店之外发布,您可以使用 Android 中较旧的定位服务(或利用它们的库),并且从应用程序中这样做是可行的,但基 Activity 类同样简单,可以进行更清晰的生命周期管理和划分位置代码。