Android的定位监听通话非常频繁 [英] Android Location Listener call very often

查看:135
本文介绍了Android的定位监听通话非常频繁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的网络位置提供商。 我需要调用的 onLocationChanged 的从我的 LocationListener的的每一次1小时方法。 这是我的code:

I am using Network Location provider. I need to call onLocationChanged method from my LocationListener only once per 1 hour. Here is my code:

MyLocationListener locationListener = new MyLocationListener();   
locationMangaer.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3600000, 0,locationListener);

不过,这是行不通的。我的 onLocationChanged 调用非常频繁。

我必须使用哪些参数?

推荐答案

在<一个href="http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates%28long,%20float,%20android.location.Criteria,%20android.app.PendingIntent%29"><$c$c>LocationManager#requestLocationUpdates()文档:

此前杰利贝恩,该minTime参数只是一个提示,有些位置的提供者实现忽略了它。从杰利贝恩及以后它是强制性为Android兼容设备,观察两者的minTime和minDistance依旧参数。

Prior to Jellybean, the minTime parameter was only a hint, and some location provider implementations ignored it. From Jellybean and onwards it is mandatory for Android compatible devices to observe both the minTime and minDistance parameters.

不过,您可以使用 requestSingleUpdate()带活套和处理程序运行一小时一次更新。

However you can use requestSingleUpdate() with a Looper and Handler to run the updates once an hour.

添加
要开始,你可以在这里阅读更多有关活套和处理程序。

Addition
To start you can read more about Loopers and Handlers here.

您正在使用的API 8这是一个不错的选择,但这种限制其LocationManager方法,我们可以调用,因为大多数是在API 9 API 8中引入的只有这三种方法:

You are using API 8 which is a good choice, but this limits which LocationManager methods we can call since most were introduced in API 9. API 8 only have these three methods:

requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)
requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener, Looper looper)
requestLocationUpdates(String provider, long minTime, float minDistance, PendingIntent intent)

让我们使用第一种方法,它是最简单的。

Let's use the first method, it is the simplest.

首先,创建LocationManager和LocationListener的像往常一样,但在 onLocationChanged()停止的要求更多更新:

First, create your LocationManager and LocationListener as you normally would, but in onLocationChanged() stop requesting more updates:

@Override
public void onLocationChanged(Location location) {
    mLocationManager.removeUpdates(mLocationListener);
    // Use this one location however you please
}

二,创建几个新类变量:

Second, create a couple new class variables:

private Handler mHandler = new Handler();
private Runnable onRequestLocation = new Runnable() {
    @Override
    public void run() {
        // Ask for a location
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
        // Run this again in an hour
        mHandler.postDelayed(onRequestLocation, DateUtils.HOUR_IN_MILLIS);
    }
};

当然,你应该禁用所有回调的的onPause() onResume再次启用它们()以prevent从后台获取未使用的更新资源浪费的LocationManager。

Of course, you ought to disable all of your callbacks in onPause() and enable them again in onResume() to prevent the LocationManager from wasting resources by acquiring unused updates in the background.

更技术一点:
如果您担心阻塞UI线程的LocationManager,那么你可以使用第二个 requestLocationUpdates()方法以提供具体的尺蠖从一个新的线程(说HandlerThread) 。

A more technical point:
If you are concerned about blocking the UI thread with the LocationManager, then you can use the second requestLocationUpdates() method to supply a specific Looper from a new Thread (say a HandlerThread).

这篇关于Android的定位监听通话非常频繁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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