Java Android位置未从自定义类返回位置
我有自己的定位课程。我有一个赔率结果,当我使用我的类搜索GPS位置时,我得到了0,0 然而,如果我在位置管理器中搜索GPS的getLastKnown函数,我会得到强制值:Java Android位置未从自定义类返回位置,java,android,geolocation,gps,locationmanager,Java,Android,Geolocation,Gps,Locationmanager,我有自己的定位课程。我有一个赔率结果,当我使用我的类搜索GPS位置时,我得到了0,0 然而,如果我在位置管理器中搜索GPS的getLastKnown函数,我会得到强制值: public Location getGPSloc(Context c){ isGPSavailable(c); if(gps_enabled) lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, locati
public Location getGPSloc(Context c){
isGPSavailable(c);
if(gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, locationListenerGps);
timerGPS = new Timer();
timerGPS.schedule(new GetLastLocationGPS(), 45000);
return gpsLoc;
}
这是我从函数外部调用的类,我在调用中使用了This.getApplicationContext。这是GetLastLocationGPS:
class GetLastLocationGPS extends TimerTask {
@Override
public void run() {
lm.removeUpdates(locationListenerGps);
if(gps_enabled){
gpsLoc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);}
else{
gpsLoc = null;
}
}
}
下面是听众:
LocationListener locationListenerGps = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
timerGPS.cancel();
gpsLoc = location;
lm.removeUpdates(this);
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
调用MyLocationInstance.getGPSlocthis.getApplicationContext时,返回值始终为0,0。我有一个发送到模拟器的强制值83 43,如果我在LocationManagerInstance.getLastKnownLocationLM.GPS中sub,它就会出现
我不知道为什么这个方法不能获取位置
这是因为在理论上,当呼叫发生时计时器仍在运行吗?这是我能想出答案的唯一办法。还有其他想法吗 我不确定您的问题是什么,但这是我的代码,它处于加载状态,功能齐全 因为这个类是从Fragment扩展而来的,所以它可以作为您的Fragment的父类。 有一个监听器,您的类可以在每个间隔后获得新位置
public class LocationFinderFragment extends Fragment implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
public interface OnNewLocationFoundListener {
public void OnNewLocationFound(Location location);
}
private static final String TAG = "LocationFinderFragment";
private static final long DEFAULT_UPDATE_LOCATION_INTERVAL = 30 * 1000; // update every 30 seconds
private static final long DEFAULT_TERMINATE_SAT_FINDING = 1 * 60 * 60 * 1000; // for 1 hour
private OnNewLocationFoundListener listener; // Listener of fragments
private OnNewLocationFoundListener listener2; // Listener of MainFragmentHolder
private Context context;
private LocationClient mLocationClient;
private static double lat = 0;
private static double lng = 0;
private static long lastTime = 0;
private LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if(location == null)
return;
if(lat == location.getLatitude() && lng == location.getLongitude()) {
Log.e(TAG, "location not changed.");
return;
}
lat = location.getLatitude();
lng = location.getLongitude();
Log.i(TAG, "Location changed to (" + lat + ", " + lng + ")");
// Ask fragment to get new data and update screen
listener.OnNewLocationFound(location);
// Display toast notification every minute (instead of every 30 seconds)
DateTime time = new DateTime();
long currentTime = time.getMillis();
if(currentTime > lastTime + 1 * 60 * 1000) {
listener2.OnNewLocationFound(location);
lastTime = currentTime;
}
}
};
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
listener2 = (OnNewLocationFoundListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnNewLocationFoundListener interface.");
}
Log.i(TAG, "Fragment attached to activity.");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Getting context
context = getActivity().getApplicationContext();
// Get location manager
mLocationClient = new LocationClient(context, this, this);
}
@Override
public void onResume() {
super.onResume();
// Get location
if(mLocationClient != null)
mLocationClient.connect();
}
@Override
public void onPause() {
super.onPause();
// remove listener in order to save resource
if(mLocationClient != null)
mLocationClient.disconnect();
}
@Override
public void onDestroy() {
super.onDestroy();
// remove listener in order to save resource
if(mLocationClient != null)
mLocationClient.disconnect();
}
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Location Connected.");
// Get last known location
Location lastLocation = mLocationClient.getLastLocation();
mLocationListener.onLocationChanged(lastLocation);
if(!SpGodMode.isLocationModeEnabled(context)) {
// Create location request
LocationRequest locationRequest = LocationRequest.create()
.setInterval(DEFAULT_UPDATE_LOCATION_INTERVAL)
.setExpirationDuration(DEFAULT_TERMINATE_SAT_FINDING)
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationClient.requestLocationUpdates(locationRequest, mLocationListener);
} else {
String strLoc = SpGodMode.getLocation(context);
if(strLoc != null) {
String lat = strLoc.substring(0, strLoc.indexOf(","));
String lng = strLoc.substring(strLoc.indexOf(",") + 1);
Location location = new Location("");
try {
location.setLatitude(Double.parseDouble(lat));
location.setLongitude(Double.parseDouble(lng));
mLocationListener.onLocationChanged(location);
} catch (NumberFormatException e) {
Log.e(TAG, "Wrong lat/lng for parsing as Double.");
Toast.makeText(context, "Wrong lat/lng", Toast.LENGTH_SHORT).show();
}
}
}
}
@Override
public void onDisconnected() {
Log.i(TAG, "Location Disconnected.");
if(mLocationClient != null && mLocationClient.isConnected())
mLocationClient.removeLocationUpdates(mLocationListener);
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e(TAG, "Connection failed listener.");
}
public void setLocationListener(OnNewLocationFoundListener listener) {
this.listener = listener;
}
public void disconnectLocation() {
mLocationClient.disconnect();
}
}
使现代化我更改了代码,以便在开始请求新位置之前,将gpsLoc定义为来自GPS提供商的getLastKnownLocation。这将返回最后一个已知值的值。现在我想起来了,不过我可能想将我的请求绑定到UI线程,以使onCreate方法保持暂停状态,直到GPSLoc完成对新位置的搜索。@DannyThunder抱歉,这是一个用于测试目的的设置类。测试人员能够硬编码他们的位置,然后应用程序基于此显示他们的位置,而不是从GPS或网络读取位置。你可以忽略那个部分…没有导入语句?