首页 > 解决方案 > FusedLocation 更新是否作为后台服务工作?

问题描述

我有一个使用FusedLocationApi将用户位置频繁发送到服务器的类。所有获取位置更新和与服务器通信的方法都在这个类中设置,除了当我想运行这个类时,我调用一个后台服务,然后在里面使用这个类。

Uisng FusedLocationApi.requestLocationUpdates,位置正在正确发送到服务器,我可以从该服务中看到Toast 。但是当我检查 android 系统上的 Running services 时,我的应用程序没有列出。

我的问题是为什么我的服务不再运行但位置正在更新?FusedLocation Service 本身是后台服务吗?我是否需要通过后台服务频繁调用它以使其保持活动状态,或者我可以在活动中调用它并让它在关闭应用程序后继续运行?

这就是我在自己的服务中使用类的方式:

public class LocationService extends Service {

    FusedClass mylocation=new FusedClass(this);

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent.getAction().equals("turnOn")) {
            HandlerThread handlerThread = new HandlerThread("LocationThread");
            handlerThread.start();

            Handler handler = new Handler(handlerThread.getLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    mylocation.LocationStartPoint();
                }
            });
        }
        if (intent.getAction().equals("turnOff")) {
            stopSelf();
        }
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        //TODO for communication return IBinder implementation
        return null;
    }

编辑:我使用FusedLocationApi.requestLocationUpdatesonLocationChanged从此 API 获取位置更新。

标签: androidfusedlocationproviderapi

解决方案


推荐阅读