在重新激活 WiFi/移动网络之前,服务中的 Android 位置侦听器无法工作

2024-04-24

我的位置侦听器工作正常,收集数据没有任何问题。但有时它不收集任何数据。我此时必须关闭并重新启动我的位置提供程序。重新启动可以解决问题,但是,这可能不是用户期望做的最好的事情。

当我使用 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);
    }
}

Usage:

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
    }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在重新激活 WiFi/移动网络之前,服务中的 Android 位置侦听器无法工作 的相关文章

随机推荐