在我重新激活 WiFi/移动网络之前,服务中的 Android 位置侦听器不起作用 [英] Android Location listener in Service does not work until I reactivate WiFi/mobile network

查看:11
本文介绍了在我重新激活 WiFi/移动网络之前,服务中的 Android 位置侦听器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My location listener works normally, collects data without any problem. But sometimes it doesn't collect any data. I have to turn off and restart my location provider at this time. Restarting fixes the problem,however, it's probably not the best thing the user is expected to do.

当我使用 GPS 作为提供者时,没有问题.

When I use GPS as provider, there is no problem.

位置侦听器在服务中工作.我无法理解问题所在.是关于 Android 还是我的代码?

Location listener works in Service. I can't understand the problem. Is it about Android or my code?

提前致谢.

推荐答案

您可以使用新的位置提供程序 (FusedLocationProvider),它结合了来自不同位置提供程序的信息,因此如果您的设备有可能获取位置,您将获得知道.当然,您应该在设备首选项中启用应用程序使用位置信息.

You could use new location provider (FusedLocationProvider), which combines info from different location providers, so if your device has any possibility to obtain location, You'll get know it. Of course, You should enable in preferences for your device to use location info by apps.

查看 developer.android.com 以获取有关此提供商的扩展信息.

Check developers.android.com for extended info about this provider.

这是对我来说很好用的解决方案:

This is solution working fine for me:

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
    }

}

这篇关于在我重新激活 WiFi/移动网络之前,服务中的 Android 位置侦听器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆