在我的应用程序中,我在应用程序打开时获取当前位置的纬度和经度,但在应用程序关闭时获取不到
我正在使用服务类获取应用程序中的当前位置纬度和经度
请告诉我如何获得当前位置的纬度和经度,即使在应用程序关闭时
几个月前,我创建了GPSTracker库来帮助我获取GPS位置。如果您需要查看GPSTracker>getLocation
演示
AndroidManifest.xml
<;使用权限android:name=“android.permission.INTERNET”/>;
<;使用权限android:name=“android.permission.ACCESS\u FINE\u LOCATION”/>;
活动
导入android.os.Bundle;
导入android.app.Activity;
导入android.view.Menu;
导入android.widget.TextView;
公共类MainActivity扩展了活动{
文本视图文本视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、地理位置);
//检查是否启用了GPS
GPSTracker GPSTracker=新的GPSTracker(本);
if(gpsTracker.getisgpstrackEnabled())
{
String stringLatitude=String.valueOf(gpsTracker.latitude);
textview=(textview)findViewById(R.id.fieldLatitude);
textview.setText(stringLatitude);
String stringLongitude=String.valueOf(gpsTracker.longitude);
textview=(textview)findViewById(R.id.field经度);
textview.setText(字符串经度);
字符串country=gpsTracker.getCountryName(this);
textview=(textview)findViewById(R.id.fieldCountry);
textview.setText(国家);
字符串city=gpsTracker.getLocality(this);
textview=(textview)findViewById(R.id.fieldCity);
textview.setText(城市);
字符串postalCode=gpsTracker.getPostalCode(this);
textview=(textview)findViewById(R.id.fieldPostalCode);
textview.setText(postalCode);
字符串addressLine=gpsTracker.getAddressLine(this);
textview=(textview)findViewById(R.id.fieldAddressLine);
textview.setText(地址行);
}
其他的
{
//找不到位置
//GPS或网络未启用
//要求用户在设置中启用GPS/网络
gpsTracker.showSettingsAlert();
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.varna\u lab\u geo\u位置,菜单);
返回true;
}
}
GPS跟踪器
导入java.io.IOException;
导入java.util.List;
导入java.util.Locale;
导入android.app.AlertDialog;
导入android.app.Service;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.location.Address;
导入android.location.Geocoder;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.os.IBinder;
导入android.provider.Settings;
导入android.util.Log;
/**
*根据教程创建此类:
* http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial
*
*对于地理编码器,请阅读以下内容:http://stackoverflow.com/questions/472313/android-reverse-geocoding-getfromlocation
*
*/
公共类GPSTracker扩展服务实现LocationListener{
//获取类名
私有静态字符串标记=GPSTracker.class.getName();
私有最终上下文mContext;
//GPS状态标志
布尔值isGPSEnabled=false;
//网络状态标志
布尔值isNetworkEnabled=false;
//GPS跟踪标志已启用
布尔值IsGPStrackEnabled=false;
位置;
双纬度;
双经度;
//有多少地理编码器应该返回我们的GPSTracker
int geocoderMaxResults=1;
//更改更新的最小距离(以米为单位)
私有静态最终长最小距离更改更新=10;//10米
//更新之间的最短时间(以毫秒为单位)
私有静态最终长MIN\u时间\u BW\u更新=1000*60*1;//1分钟
//声明位置管理器
受保护的LocationManager LocationManager;
//存储LocationManager.GPS\提供程序或LocationManager.NETWORK\提供程序信息
私有字符串提供者信息;
公共GPSTracker(上下文){
this.mContext=上下文;
getLocation();
}
/**
*尝试通过GPS或网络提供商获取我的当前位置
*/
public void getLocation(){
试一试{
locationManager=(locationManager)mContext.getSystemService(LOCATION\u服务);
//获取GPS状态
isGPSEnabled=locationManager.isProviderEnabled(locationManager.GPS\U提供程序);
//获取网络状态
isNetworkEnabled=locationManager.isProviderEnabled(locationManager.NETWORK\u提供程序);
//如果启用了GPS服务,请尝试获取位置
如果(isGPSEnabled){
this.isgpstrackEnabled=true;
Log.d(标签“应用程序使用GPS服务”);
/*
*此提供程序使用
*卫星。根据情况,此提供商可能需要一段时间才能返回
*定位。
*/
provider\u info=LocationManager.GPS\u provider;
}否则,如果(isNetworkEnabled){//如果启用了网络服务,请尝试获取位置
this.isgpstrackEnabled=true;
Log.d(标记“应用程序使用网络状态获取GPS坐标”);
/*
*此提供程序根据以下内容确定位置:
*蜂窝塔和WiFi接入点的可用性。检索结果
*通过网络查找。
*/
provider\u info=LocationManager.NETWORK\u provider;
}
//应用程序可以使用GPS或网络提供商
如果(!provider_info.isEmpty()){
locationManager.RequestLocationUpdate(
供应商信息,
最短时间更新,
用于更新的最小距离更改,
这
);
如果(locationManager!=null){
location=locationManager.getLastKnownLocation(提供者信息);
updateGPSCoordinates();
}
}
}
捕获(例外e)
{
//e、 printStackTrace();
Log.e(标签“无法连接到LocationManager”,e);
}
}
/**
*更新GPSTracker纬度和经度
*/
public void updategpscordinates(){
如果(位置!=null){
纬度=位置。getLatitude();
longitude=location.getLongitude();
}
}
/**
*GPSTracker纬度获取和设置器
*@返回纬度
*/
公共双纬度(){
如果(位置!=null){
纬度=位置。getLatitude();
}
返回纬度;
}
/**
*GPSTracker经度获取和设置器
*@返回
*/
公共双getLongitude(){
如果(位置!=null){
longitude=location.getLongitude();
}
返回经度;
}
/**
*GPSTracker是GPStrackEnabled getter。
*检查GPS/wifi是否已启用
*/
公共布尔值GetIsGPStrackEnabled(){
返回此项。isGPSTrackingEnabled;
}
/**
*停止使用GPS侦听器
*调用此方法将停止在应用程序中使用GPS
*/
使用GPS()的公共无效停止{
如果(locationManager!=null){
locationManager.RemoveUpdate(GPSTracker.this);
}
}
/**
*用于显示设置警报对话框的函数
*/
公共void showSettingsAlert(){
AlertDialog.Builder AlertDialog=新建AlertDialog.Builder(mContext);
//设置对话框标题
setTitle(R.string.GPSAlertDialogTitle);
//设置对话框消息
alertDialog.setMessage(R.string.GPSAlertDialogMessage);
//按下设置按钮时
alertDialog.setPositiveButton(R.string.action_设置,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which)
{
意向意向=新意向(设置、动作、位置、来源、设置);
mContext.startActivity(意图);
}
});
//按取消按钮时
alertDialog.setNegativeButton(R.string.cancel,新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which)
{
dialog.cancel();
}
});
alertDialog.show();
}
/**
*按纬度和经度获取地址列表
*@return null或List<;Address>;
*/
公共列表<;地址>;getGeocoderAddress(上下文){
如果(位置!=null){
Geocoder Geocoder=新的Geocoder(上下文,Locale.ENGLISH);
试一试{
/**
*Geocoder.getFromLocation-返回地址数组
*用来描述给定纬度和经度周围的区域。
*/
List<;Address>;addresses=geocoder.getFromLocation(纬度、经度、this.geocoderMaxResults);
返回地址;
}捕获(IOE异常){
//e、 printStackTrace();
Log.e(标签,“无法连接到Geoco