onLocationChanged从未调用Emulator package com.ecsmon.android.core; 导入静态com.ecsmon.android.constants.constants.log; 导入java.io.IOException; 导入java.util.List; 导入android.annotation.SuppressLint; 导入android.content.Context; 导入android.location.Address; 导入android.location.Criteria; 导入android.location.Geocoder; 导入android.location.location; 导入android.location.LocationListener; 导入android.location.LocationManager; 导入android.os.Bundle; @SuppressLint(“新API”) 公共类GPSManager{ 专用双电流纬度=0d; 专用双电流经度=0d; 私有静态上下文mCtx; 私有位置lastLocationBestProvider=null; 私人场所经理; 专用GPSListenerImpl mGPSListener; 私有com.ecsmon.android.core.LocationListener mOutListener; 启用私有布尔值=false; 专用GPSListenerImpl mNETListener; 公共GPSManager(Context ctx,com.ecsmon.android.core.LocationListener LocationListener){ mCtx=ctx; mlLocationManager=(LocationManager)ctx.getSystemService(Context.LOCATION\u服务); mOutListener=locationListener; } /** *开始位置更新 */ 公开作废开始(){ 日志(“开始跟踪”); lastLocationBestProvider=getLastLocationFromBestProvider(); if(lastLocationBestProvider!=null){ currentLatitude=lastLocationBestProvider.getLatitude(); currentLongitude=lastLocationBestProvider.getLongitude(); 对数(“纬度”+当前纬度+“长”+当前经度); }否则{ 日志(“最后一个动作为空”); } //mGPSListener=新的GPSListenerImpl(“GPS”); mNETListener=新的GPSListenerImpl(“净”); mlLocationManager.RequestLocationUpdate(LocationManager.NETWORK_提供程序,0,1,mNETListener); //mlLocationManager.RequestLocationUpdate(LocationManager.GPS_提供程序,0,1,mGPSListener); } 私有类GPSListenerImpl实现LocationListener{ 私有字符串名称=”; 公共GPSListenerImpl(字符串名称){ 日志(“创建的侦听器”+名称); this.name=名称; } 位置更改后的公共无效(位置loc){ 日志(“!!!!!!!!!!!!!”; 如果(loc!=null){ 日志(“位置已更改”+loc.getAccurance()); currentLatitude=loc.getLatitude(); currentLength=loc.getLength(); mOutListener.update(当前经度、当前纬度); }否则{ 日志(“位置为空”); } } 公共无效onProviderDisabled(字符串提供程序){ 日志(“提供程序已禁用>”+名称); } 公共无效onProviderEnabled(字符串提供程序){ 日志(“已启用提供程序>”+名称); } public void onStatusChanged(字符串提供程序、int状态、Bundle extra){ 日志(“状态更改”); } } /** *返回保存在手机中的最后位置或null * *@返回位置 */ 公共位置getLastLocationFromBestProvider(){ 如果(!已启用){ 返回null; } 试一试{ LocationManager lm=(LocationManager)mCtx.getSystemService(Context.LOCATION\u服务); 标准=新标准(); 标准.设定精度(标准.精度\u粗略); 标准。setAltitudeRequired(false); 标准。需要设置(假); 条件.setCostAllowed(true); 字符串strLocationProvider=lm.getBestProvider(条件为true); 位置=lm.getLastKnownLocation(strLocationProvider); 如果(位置!=null){ 返回位置; } 返回null; }捕获(例外e){ 日志(如getMessage()); 返回null; } } /** *从经度和纬度返回人类可读的地址 * *@param纬度 *@param经度 *@返回 */ 公共字符串getAddress(双纬度、双经度){ 如果(!已启用){ 返回null; } 字符串m=”“; 试一试{ 如果(!Geocoder.isPresent()){ 返回null; } 地理编码器geo=新地理编码器(mCtx); 列表地址=geo.getFromLocation(纬度,经度,1); if(addresses.isEmpty()){ 返回null; }否则{ 如果(地址.size()>0){ m=addresses.get(0.getFeatureName()+”,“+地址.get(0.GetLocation()+”,” +addresses.get(0.getCountryName(); } } }捕获(IOIE){ 日志(“无连接”); 返回null; }捕获(例外e){ 日志(“无法从该坐标读取地址:lat=“+纬度+”长“+经度”)// 返回null; } 返回m; } /** *删除所有位置更新 */ 公共停车场(){ 试一试{ mLocationManager.RemoveUpdate(mGPSListener); mLocationManager.RemoveUpdate(mNETListener); }捕获(例外e){ } } }
这是我获取当前位置的主要类,onLocationChanged从未被调用。Im在模拟器上测试,并通过模拟器控件发送模拟经度和纬度。请帮助我,这让我抓狂:(问题在于emulator无法工作,但在真实设备上,它在几秒钟内找到了我的位置您可以使用onLocationChanged从未调用Emulator package com.ecsmon.android.core; 导入静态com.ecsmon.android.constants.constants.log; 导入java.io.IOException; 导入java.util.List; 导入android.annotation.SuppressLint; 导入android.content.Context; 导入android.location.Address; 导入android.location.Criteria; 导入android.location.Geocoder; 导入android.location.location; 导入android.location.LocationListener; 导入android.location.LocationManager; 导入android.os.Bundle; @SuppressLint(“新API”) 公共类GPSManager{ 专用双电流纬度=0d; 专用双电流经度=0d; 私有静态上下文mCtx; 私有位置lastLocationBestProvider=null; 私人场所经理; 专用GPSListenerImpl mGPSListener; 私有com.ecsmon.android.core.LocationListener mOutListener; 启用私有布尔值=false; 专用GPSListenerImpl mNETListener; 公共GPSManager(Context ctx,com.ecsmon.android.core.LocationListener LocationListener){ mCtx=ctx; mlLocationManager=(LocationManager)ctx.getSystemService(Context.LOCATION\u服务); mOutListener=locationListener; } /** *开始位置更新 */ 公开作废开始(){ 日志(“开始跟踪”); lastLocationBestProvider=getLastLocationFromBestProvider(); if(lastLocationBestProvider!=null){ currentLatitude=lastLocationBestProvider.getLatitude(); currentLongitude=lastLocationBestProvider.getLongitude(); 对数(“纬度”+当前纬度+“长”+当前经度); }否则{ 日志(“最后一个动作为空”); } //mGPSListener=新的GPSListenerImpl(“GPS”); mNETListener=新的GPSListenerImpl(“净”); mlLocationManager.RequestLocationUpdate(LocationManager.NETWORK_提供程序,0,1,mNETListener); //mlLocationManager.RequestLocationUpdate(LocationManager.GPS_提供程序,0,1,mGPSListener); } 私有类GPSListenerImpl实现LocationListener{ 私有字符串名称=”; 公共GPSListenerImpl(字符串名称){ 日志(“创建的侦听器”+名称); this.name=名称; } 位置更改后的公共无效(位置loc){ 日志(“!!!!!!!!!!!!!”; 如果(loc!=null){ 日志(“位置已更改”+loc.getAccurance()); currentLatitude=loc.getLatitude(); currentLength=loc.getLength(); mOutListener.update(当前经度、当前纬度); }否则{ 日志(“位置为空”); } } 公共无效onProviderDisabled(字符串提供程序){ 日志(“提供程序已禁用>”+名称); } 公共无效onProviderEnabled(字符串提供程序){ 日志(“已启用提供程序>”+名称); } public void onStatusChanged(字符串提供程序、int状态、Bundle extra){ 日志(“状态更改”); } } /** *返回保存在手机中的最后位置或null * *@返回位置 */ 公共位置getLastLocationFromBestProvider(){ 如果(!已启用){ 返回null; } 试一试{ LocationManager lm=(LocationManager)mCtx.getSystemService(Context.LOCATION\u服务); 标准=新标准(); 标准.设定精度(标准.精度\u粗略); 标准。setAltitudeRequired(false); 标准。需要设置(假); 条件.setCostAllowed(true); 字符串strLocationProvider=lm.getBestProvider(条件为true); 位置=lm.getLastKnownLocation(strLocationProvider); 如果(位置!=null){ 返回位置; } 返回null; }捕获(例外e){ 日志(如getMessage()); 返回null; } } /** *从经度和纬度返回人类可读的地址 * *@param纬度 *@param经度 *@返回 */ 公共字符串getAddress(双纬度、双经度){ 如果(!已启用){ 返回null; } 字符串m=”“; 试一试{ 如果(!Geocoder.isPresent()){ 返回null; } 地理编码器geo=新地理编码器(mCtx); 列表地址=geo.getFromLocation(纬度,经度,1); if(addresses.isEmpty()){ 返回null; }否则{ 如果(地址.size()>0){ m=addresses.get(0.getFeatureName()+”,“+地址.get(0.GetLocation()+”,” +addresses.get(0.getCountryName(); } } }捕获(IOIE){ 日志(“无连接”); 返回null; }捕获(例外e){ 日志(“无法从该坐标读取地址:lat=“+纬度+”长“+经度”)// 返回null; } 返回m; } /** *删除所有位置更新 */ 公共停车场(){ 试一试{ mLocationManager.RemoveUpdate(mGPSListener); mLocationManager.RemoveUpdate(mNETListener); }捕获(例外e){ } } },android,android-emulator,location,Android,Android Emulator,Location,这是我获取当前位置的主要类,onLocationChanged从未被调用。Im在模拟器上测试,并通过模拟器控件发送模拟经度和纬度。请帮助我,这让我抓狂:(问题在于emulator无法工作,但在真实设备上,它在几秒钟内找到了我的位置您可以使用emulator control更改emulator的GPS位置值。通过这样做(onLocationChanged方法可以工作)你可以在Emulator中测试你的应用程序,Stefan是对的。你需要做两件事 授予对模拟位置的访问权。目前,这需要在src/deb
emulator control
更改emulator的GPS位置值。通过这样做(onLocationChanged
方法可以工作)你可以在Emulator中测试你的应用程序,Stefan是对的。你需要做两件事
package com.ecsmon.android.core;
import static com.ecsmon.android.constants.Constants.log;
import java.io.IOException;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
@SuppressLint("NewApi")
public class GPSManager {
private double currentLatitude = 0d;
private double currentLongitude = 0d;
private static Context mCtx;
private Location lastLocationBestProvider = null;
private LocationManager mLocationManager;
private GPSListenerImpl mGPSListener;
private com.ecsmon.android.core.LocationListener mOutListener;
private boolean enabled = false;
private GPSListenerImpl mNETListener;
public GPSManager(Context ctx, com.ecsmon.android.core.LocationListener locationListener) {
mCtx = ctx;
mLocationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
mOutListener = locationListener;
}
/**
* Start location updates
*/
public void start() {
log("#### Started tracking");
lastLocationBestProvider = getLastLocationFromBestProvider();
if (lastLocationBestProvider != null) {
currentLatitude = lastLocationBestProvider.getLatitude();
currentLongitude = lastLocationBestProvider.getLongitude();
log("lat" + currentLatitude + " long " + currentLongitude);
} else {
log("last loaction is null");
}
// mGPSListener = new GPSListenerImpl("GPS");
mNETListener = new GPSListenerImpl("NET");
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 1, mNETListener);
// mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, mGPSListener);
}
private class GPSListenerImpl implements LocationListener {
private String name = "";
public GPSListenerImpl(String name) {
log("listener created" + name);
this.name = name;
}
public void onLocationChanged(Location loc) {
log("######### LOCATION CHANGED CALLED!!!!!!!!!!!!!!!!! ##############");
if (loc != null) {
log("######### location changed " + loc.getAccuracy());
currentLatitude = loc.getLatitude();
currentLongitude = loc.getLongitude();
mOutListener.update(currentLongitude, currentLatitude);
} else {
log("location is null");
}
}
public void onProviderDisabled(String provider) {
log("provider disabled > " + name);
}
public void onProviderEnabled(String provider) {
log("provider enabled > " + name);
}
public void onStatusChanged(String provider, int status, Bundle extras) {
log("status changed");
}
}
/**
* Return last location saved in phone or null
*
* @return Location
*/
public Location getLastLocationFromBestProvider() {
if (!enabled) {
return null;
}
try {
LocationManager lm = (LocationManager) mCtx.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
String strLocationProvider = lm.getBestProvider(criteria, true);
Location location = lm.getLastKnownLocation(strLocationProvider);
if (location != null) {
return location;
}
return null;
} catch (Exception e) {
log(e.getMessage());
return null;
}
}
/**
* Returns human readable address from longitude and latitude
*
* @param latitude
* @param longitude
* @return
*/
public String getAddress(Double latitude, Double longitude) {
if (!enabled) {
return null;
}
String m = "";
try {
if (!Geocoder.isPresent()) {
return null;
}
Geocoder geo = new Geocoder(mCtx);
List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
if (addresses.isEmpty()) {
return null;
} else {
if (addresses.size() > 0) {
m = addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() + ", "
+ addresses.get(0).getCountryName();
}
}
} catch (IOException ie) {
log("No connection.");
return null;
} catch (Exception e) {
log("Can't read adress from this cordinates : lat = " + latitude + " long " + longitude); //
return null;
}
return m;
}
/**
* Removes all location updates
*/
public void stop() {
try {
mLocationManager.removeUpdates(mGPSListener);
mLocationManager.removeUpdates(mNETListener);
} catch (Exception e) {
}
}
}