我的位置监听器工作正常,收集数据没有任何问题。但有时它不会收集任何数据。此时我必须关闭并重新启动我的位置提供程序。重新启动可以解决问题,但是,这可能不是用户期望做的最好的事情。
当我使用 GPS 作为提供者时,没有问题。
位置侦听器在服务中工作。我无法理解这个问题。是关于 Android 还是我的代码?
提前致谢。
您可以使用新的位置提供者(FusedLocationProvider),它结合了来自不同位置提供者的信息,因此如果您的设备有任何获取位置的可能性,您就会知道。当然,您应该在首选项中为您的设备启用应用程序使用位置信息。
检查 developers.android.com 以获取有关此提供程序的扩展信息。
这是对我来说很好的解决方案:
public class FusedLocationListener implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {
public interface LocationListener {
public void onReceiveLocation(Location location);
}
private LocationListener mListener;
public static final String TAG = "Fused";
private LocationClient locationClient;
private LocationRequest locationRequest;
protected int minDistanceToUpdate = 1000;
protected int minTimeToUpdate = 10*1000;
protected Context mContext;
@Override
public void onConnected(Bundle bundle) {
Log.d(TAG, "Connected");
locationRequest = new LocationRequest();
locationRequest.setSmallestDisplacement(1);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30000);
locationRequest.setNumUpdates(1);
locationClient.requestLocationUpdates(locationRequest, this);
}
@Override
public void onDisconnected() {
Log.d(TAG, "Disconnected");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "Failed");
}
private static FusedLocationListener instance;
public static synchronized FusedLocationListener getInstance(Context context, LocationListener listener){
if (null==instance) {
instance = new FusedLocationListener(context, listener);
}
return instance;
}
private FusedLocationListener(Context context, LocationListener listener){
mContext = context;
mListener = listener;
}
public void start(){
Log.d(TAG, "Listener started");
locationClient = new LocationClient(mContext,this,this);
locationClient.connect();
}
@Override
public void onLocationChanged(Location location) {
Log.d(TAG, "Location received: " + location.getLatitude() + ";" + location.getLongitude());
//notify listener with new location
mListener.onReceiveLocation(location);
}
public void stop() {
locationClient.removeLocationUpdates(this);
}
}
用法:
public class MyActivity extends Activity implements FusedLocationListener.LocationListener {
@Override
public void onCreate(Bundle savedInstanceState) {
FusedLocationListener locationListener FusedLocationListener.getInstance(getApplicationContext(), this);
locationListener.start();
}
@Override
public void onReceiveLocation(Location location) {
//handle location here
}
}
为什么不同时使用 *GPS_PROVIDER* 和 *NETWORK_PROVIDER* ?我试过了,它工作正常。
在其他设备上或仅在您的设备上进行测试时是否会发生此错误?
你可以为我创建检测这两个我已经使用了 GPS_PROVIDER 和 NETWORK_PROVIDER 如果它在 20 秒后无法检测到任何 GPS,它将尝试使用 NETWORK 提供程序
///首先创建MYlocation类
public class MyLocation {
Timer timer1;
LocationManager lm;
LocationResult locationResult;
boolean gps_enabled=false;
boolean network_enabled=false;
public boolean getLocation(Context context, LocationResult result)
{
//I use LocationResult callback class to pass location value from MyLocation to user code.
locationResult=result;
if(lm==null)
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//exceptions will be thrown if provider is not permitted.
try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}
//don't start listeners if no provider is enabled
if(!gps_enabled && !network_enabled)
return false;
if(gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
if(network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
timer1=new Timer();
timer1.schedule(new GetLastLocation(), 20000);
return true;
}
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerNetwork);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
class GetLastLocation extends TimerTask {
@Override
public void run() {
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);
Location net_loc=null, gps_loc=null;
if(gps_enabled)
gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(network_enabled)
net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//if there are both values use the latest one
if(gps_loc!=null && net_loc!=null){
if(gps_loc.getTime()>net_loc.getTime())
locationResult.gotLocation(gps_loc);
else
locationResult.gotLocation(net_loc);
return;
}
if(gps_loc!=null){
locationResult.gotLocation(gps_loc);
return;
}
if(net_loc!=null){
locationResult.gotLocation(net_loc);
return;
}
locationResult.gotLocation(null);
}
}
public void cancelTimer()
{ timer1.cancel();
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);
}
public static abstract class LocationResult{
public abstract void gotLocation(Location location);
}
}
/进入你的主课,你可以像这样使用它,我对 webview 暗示
public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView=(WebView)findViewById(R.id.webview);
final String string_loc = getIntent().getExtras().getString("nearest");
LocationResult locationResult = new LocationResult(){
@Override
public void gotLocation(Location location) {
passLat=Double.toString(location.getLatitude());
passLong=Double.toString(location.getLongitude());
maps="http://maps.google.com/maps?q="+string_loc+"+loc:"+passLat+","+passLong+"&hq=hospital&t=m&z=12";
mWebView.loadUrl(maps);
}
};
myLocation.getLocation(this, locationResult);
}
}
}
//希望对你有帮助
使用网络提供商需要互联网连接才能工作。它从小区和 wifi 热点收集一些信息。然后它查询一个后端服务器,该服务器查找它的数据库并给出一个大概的位置。该设备可以预取一些位置信息,因此您不需要持续的互联网连接,但是当设备移出预取位置时,它将再次需要互联网连接。
这可能是您的问题,您有时会失去互联网连接(wifi 进入睡眠状态)并重新启动提供商使其重新连接到互联网。同样,这充其量只是我的猜测:)