Android的定期GPS定位的更新与AlarmManager服务内 [英] Android Periodic GPS location updates with AlarmManager inside a Service

查看:369
本文介绍了Android的定期GPS定位的更新与AlarmManager服务内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了很多关于这里的问题,但无法弄清楚的问题是什么。

I read a lot of questions on here but couldn't figure out what the problem is.

我正在写一个现场服务的应用程序为Android。在活动(MyActivity.java)之一,我有两个按钮:启动和停止。

I'm writing a field service application for Android. In one of the Activities (MyActivity.java) I have two buttons: Start and Stop.

在现场工作人员presses开始我要得到他的当前位置与GPS并将其发送到及时区间服务器(说为5分钟默认情况下,在这里我将它设置为20秒。用于测试)。客户希望该看到,工人们有多长花时间在交通,在我的城市交通(伊斯坦布尔)是一个烂摊子。

When the field worker presses start I need to get his current location with GPS and send it to server in timely intervals (say it be 5 minutes default, here I set it to 20 secs. for testing). The customer wants this to see how long the workers spend time in traffic, the traffic in my city (Istanbul) is a mess.

我有一个AlarmManager在我的活动,当启动按钮是pressed警报是由 AlarmManager.setRepeating设置()来启动服务(ServiceClass。 JAVA)。

I have an AlarmManager in my activity, when the start button is pressed the alarm is set by AlarmManager.setRepeating() to start the service (ServiceClass.java).

Intent intent = new Intent (MyActivity.this, ServiceClass.class);
mPendingIntent = PendingIntent.getService(MyActivity.this, 0, intent,
                                              PendingIntent.FLAG_CANCEL_CURRENT);
mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
mAlarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 20*1000,
                                                                  mPendingIntent);

我取消与停止按钮报警。

I cancel the Alarm with the Stop button.

它完美到这里,在服务启动。其的onCreate() ONSTART()的onDestroy()方法被执行。但我不能得到的位置。我不工作一个真正的装置上,所以我用DDMS发送位置。但是,应用程序将跳过该步骤并打印经度和我的位置的纬度经度:0纬度:0。

It works perfectly till here, the Service starts. Its onCreate(), onStart() and onDestroy() methods are executed. But I can't get the location. I'm not working on a real device so I'm using DDMS to send locations. But the application skips that step and prints Longitude and Latitude of my location as Lon:0 Lat:0.

下面是服务里面的code:

Here's the code inside the service:

public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    Log.d("Testing", "Service got started, calling location updates:");
    mLocationManager.requestSingleUpdate(mCriteria, mLocationListener,
                                                                getMainLooper());
    Log.i("TESTING LOCATION UPDATE: LOCATION:", "\nLat:"   + mLatitude +
                                                          "  Lon:" + mLongitude);
    stopSelf(startId);
    Log.d("Testing", "Service Stopped!");

最后,这里是我的LocationListener的:

And finally, here's my LocationListener:

 mLocationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {

            if(location != null){
                mLatitude = location.getLatitude();
                mLongitude = location.getLongitude();
                Log.i("onLocationChanged(Location location)", "Lat:"+mLatitude + "
                                                            Lon:" + mLongitude);
}

还有什么吸引我的目光,当我运行code:

Also something caught my eye, when I run the code:

mLocationProvider = mLocationManager.getBestProvider(mCriteria, true);
Log.i("BEST PROVIDER IS:", mLocationProvider);

报告说,最佳的供应者是:GPS。但是这个日志里onProviderEnabled()再也不能在logcat中所示。

It says BEST PROVIDER IS: gps. But this log inside onProviderEnabled() is never shown in logcat.

@Override
public void onProviderEnabled(String s) {
    Log.v("onProviderEnabled", "ENABLED");
}

有两点需要补充,如果我使用:

Two things to add, if I use:

mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                                                              mLocationListener);

这也不行。

然而,这工作,我可以得到LastKnownLocation:

This however works and I can get the LastKnownLocation:

Location lastKnown = mLocationManager.getLastKnownLocation(
                                                   LocationManager.GPS_PROVIDER);

首先,这是做一个很好的方法吗?如果是,请检查一下,我失去了什么。如果不是,可以请你告诉我,我怎么能实现这个更好?

First of all, is this a good approach for doing this? If it is please check to see what I am missing. If it is not, can you please tell me how can I implement this any better?

感谢您。 :)

推荐答案

首先,你不能注册位置更新,然后关闭该服务。在最好的情况,你会泄漏线程。在最坏情况下,Android将终止进程(思维有什么在它运行的),你不会得到更新。此外,GPS需要一段时间来热身,所以你可能无法得到修复的20秒内,尤其是在城市环境。此外,您必须确保当你试图收集固定装置保持清醒。

First, you cannot register for location updates, then shut down the service. At best, you will leak threads. At worst, Android will terminate your process (thinking there is nothing running in it) and you will not get the update. Also, GPS takes a while to warm up, so you might not get a fix within 20 seconds, particularly in an urban setting. Also, you have to make sure the device stays awake while you are trying to collect a fix.

其结果是,获得在后台定期位置锁定是一个相当复杂的问题。

As a result, getting periodic location fixes in the background is a rather complicated problem.

我写了 LocationPoller 设法解决这个问题,和另一家开发商已经分叉和扩展它。您可以直接使用这些或简单地把它们作为思想的来源。

I wrote a LocationPoller to try to address this, and another developer has forked and extended it. You could use these directly or simply use them as a source of ideas.

这篇关于Android的定期GPS定位的更新与AlarmManager服务内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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