【问题标题】:Best way to give Location information to a service将位置信息提供给服务的最佳方式
【发布时间】:2013-04-02 12:29:42
【问题描述】:

我已经搜索了很多,但我并非完全一无所知。我已经实施了一个临时解决方案,但想知道是否有更好的方法。

我有一个应用程序,它每 60 秒将一个人的位置发送到服务器。在我的仪表板上(应用程序启动后将转到 onPause 的主屏幕),我使用以下代码注册了一个 LocationManager

service = (LocationManager) getSystemService(LOCATION_SERVICE);
        boolean enabled = service
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        if (!enabled)
        {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
        else
        {
            Criteria criteria = new Criteria();
            provider = service.getBestProvider(criteria, false);
            service.requestLocationUpdates(provider, 10000, 50, this);

            Location location = service.getLastKnownLocation(provider);

            // Initialize the location fields
            if (location != null)
            {
                onLocationChanged(location);
            }
            else
            {
                Log.d("Location: ", "No update received");
            }
        }

但是,正如我所提到的,用户将最小化此活动(通过按主页按钮)。 AlarmManager 每 60 秒调用一次服务。该服务从 Dashboard Activity(纬度、经度)访问静态变量并将其发送到服务器。

我的问题:

如果活动继续暂停,requestLocationUpdates 函数会停止吗?还是会继续工作?

如果它继续工作,它将不断更新两个 lat 和 lon 静态 String 对象,并且服务将不断获取更新的值。如果它们停止,服务将一次又一次地获取相同的旧值。

另外,有没有更好的方法来解决这个问题?混合使用 GPS 提供商和网络提供商? (我需要相当准确的值)。

编辑

这是我的闹钟。此代码在 Login Activity

Intent i = new Intent(con, LocationPoller.class);
                i.putExtra(LocationPoller.EXTRA_INTENT, new Intent(con,
                        Login.class));
                i.putExtra(LocationPoller.EXTRA_PROVIDER,
                        LocationManager.GPS_PROVIDER);

                gps = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

                PendingIntent pi = PendingIntent.getBroadcast(con, 0, i, 0);
                gps.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
                        10 * 1000, pi);
                Log.d("Service: ",
                        "GPS Service started and scheduled with AlarmManager");

这是我的接收器(也在登录活动中)

private class ReceiveMessages extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            Location loc = (Location) intent.getExtras().get(
                    LocationPoller.EXTRA_LOCATION);

            String msg;

            if (loc == null)
            {
                msg = intent.getStringExtra(LocationPoller.EXTRA_ERROR);
            }
            else
            {
                msg = loc.toString();
            }

            if (msg == null)
            {
                msg = "Invalid broadcast received!";
            }

            Log.d("GPS Broadcast: ", msg);
        }
    }

什么都没有发生:s 在 logcat 上没有得到任何东西,这意味着广播没有被接收到。

【问题讨论】:

    标签: android gps android-location


    【解决方案1】:

    当活动暂停时,所有注册的侦听器都将停止。更好的实现方法是,警报管理器每 60 秒发送一次广播,该广播接收器启动一个服务,该服务将在 Wakeful 线程上请求一个位置,一旦检索到位置信息,就更新服务器上的位置。

    有一个可用的开源库和一个示例(由 CommonsWare 提供),请参阅下面的链接。它在 Apache 2.0 许可下

    Location Polling Library

    请使用上述库找到我的示例项目。我在上面的库中修改了一些东西并创建了我自己的版本。

    Location Polling Demo Application

    【讨论】:

    • 这个例子对于像这样微不足道的任务来说太复杂了。
    • @Asim - 我假设您想要不断获取位置并更新到服务器。如果是这种情况,那么您需要这种复杂性。例如,如果手机处于睡眠模式并且您要上传位置信息。为此,您需要获取唤醒锁,获取并上传位置,然后释放唤醒锁。如果您的需要只是在活动处于前台时获取和更新位置,那么您可以通过从活动中启动服务来获取位置并将其更新到服务器来实现此目的
    • 好的,我已经导入了库。我现在有点困惑(BroadcastReceiver 完全是新手)。我正在登录屏幕上启动警报。当用户登录(用户登陆仪表板)时,该屏幕进入 onResume (或其他)。 RECEIVER 应该在哪个活动中?此外,我的仪表板活动已经将接收器用于另一项任务。如何区分多个广播?我知道意图.. 但是这个广播的起始意图是 Intent(Login, Dashboard),但是我怎样才能在我的 Dashboard 活动中使用它呢?对不起,如果这一切都非常令人困惑:)
    • 您的 AlarmManager 应该使用 PendingIntent.getBroadcast()。因此,当警报响起时,它将向您在 PendingIntent 中定义的接收器发送广播
    猜你喜欢
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    相关资源
    最近更新 更多