【问题标题】:LocationClient Timer in Service with WakeLock使用 WakeLock 服务的 LocationClient 计时器
【发布时间】:2013-06-28 18:19:51
【问题描述】:

上下文:如何创建某种 Timer/AlarmManger 机制,允许 LocationClient 在轮询位置之前连接两分钟。下面我粘贴了我在轮询位置之前延迟两分钟的尝试,但它在睡眠模式下似乎不起作用。

可能的解决方案:我可以使用 AlarmManager。然而,这将迫使我创建一个 SECOND 服务,然后使引用单个 LocationClient 变得非常复杂。我也可以按照这个人的方法(Using Google Play Services LocationClient in background service),在 onConnected 方法中,他执行位置轮询。

我会喜欢关于我应该研究什么的任何建议

在我的 onStartCommand 方法中...

wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");

    wl.acquire();

    //Getting and Posting Location 
    Log.i("localwordservice", "Creating and Connecting mLocationClient");
    mLocationClient = new LocationClient(this, this, this);
    mLocationClient.connect();

    Timer theTimer = new Timer();
    theTimer.schedule(new TimerTask() {
        @Override
        public void run() {
            if(checkIfGooglePlay()) {
                System.out.println("TIMER is now iniating post location");
                getPostLocation();
                System.out.println("wake lock being released!");
                wl.release();
                System.out.println("The Service is being stopped!");
                stopSelf();
            }
        }
    }, TWO_MINUTES);

【问题讨论】:

    标签: android location android-service google-play android-alarms


    【解决方案1】:

    这不是 LocationClient 的工作方式。相反,您应该等待两分钟,连接到位置服务,然后通过调用 getLastLocation() 获取当前位置。或者,您可以立即调用 requestLocationUpdates() 并持续 2 分钟,然后关闭连接或关闭更新。

    【讨论】:

    • 嗨@Joe,我认为需要定义LocationClient,告诉它连接,等待一段时间以获得良好的连接,通过.getLastLocation() 使用该连接,然后断开连接。非常感谢您的反馈!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 2015-02-06
    • 1970-01-01
    相关资源
    最近更新 更多