【问题标题】:Android: User location without locationListenerAndroid:没有 locationListener 的用户位置
【发布时间】:2011-09-04 03:56:54
【问题描述】:

我想知道是否可以在不使用 LocationListener 的情况下获取用户位置。

我问的原因是我的 locationListener 事件没有被调用。

public class LocationActivity extends Activity implements LocationListener{

    private LocationManager m_locationManager;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        LinearLayout content = new LinearLayout(this);
        content.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        content.setOrientation(LinearLayout.VERTICAL);
        content.setBackgroundColor(Color.TRANSPARENT);

        TextView infoLabel = new TextView(this);
        infoLabel.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        infoLabel.setTextSize(20.0f);
        infoLabel.setText("Initializing...");
        content.addView(infoLabel);

        try{
            m_locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            String provider = null;

            if ( m_locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
                provider = LocationManager.GPS_PROVIDER ;
                Log.d("Unity", "Using GPS");
                m_locationManager.requestLocationUpdates(provider, 0, 0, this);
            } else if(m_locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                provider = LocationManager.NETWORK_PROVIDER;
                Log.d("Unity", "Using Netword");
                m_locationManager.requestLocationUpdates(provider, 0, 0, this);
            } else {
                Log.d("Unity", "Provider Not available");
            }

        }catch(Exception ex){
            Log.d("Unity", "locatons error " + ex.getMessage());
        }

        setContentView(content);

    }

    public void onLocationChanged(Location location) {
        Log.d("Unity", "UserLocation Lat:" + location.getLatitude() + " Long:" + location.getLongitude());
    }
    public void onStatusChanged(String provider, int status, Bundle extras) {Log.d("Unity", "onStatusChanged");}
    public void onProviderEnabled(String provider) {Log.d("Unity", "onProviderEnabled");}
    public void onProviderDisabled(String provider) {Log.d("Unity", "onProviderDisabled");}
} 

查看这篇文章了解更多信息:

Android: Can't create handler inside thread that has not called Looper.prepare()

修改以反映 mmeyer cmets

【问题讨论】:

    标签: android locationlistener


    【解决方案1】:

    首先,您可以在使用LocationManager.getLastKnownLocation(provider) 选择提供程序时检查最后一个已知位置,看看它是否不为空且不是太旧。

    除此之外,您的代码看起来已经足够了。我怀疑问题可能是您对 requestLocationUpdates 的调用中的第三个参数表示仅在位置更改超过 100 米时才发送更新。对于大多数在调试和观察 logcat 的情况下,每秒移动一百米似乎不太可能。

    来自 API 文档:

    通知的频率可以使用 minTime 和 minDistance 参数。如果 minTime 大于 0,则 LocationManager 可能会休息 minTime 毫秒 在位置更新之间以节省电力。 如果 minDistance 更大 大于 0,仅当设备移动时才会广播位置 minDistance 米。 要尽可能频繁地获取通知, 将两个参数都设置为 0。

    【讨论】:

    • 所以 getLastKnownLocation 不为空,但仍然没有更新。您是否知道不使用 locationListener 可以获取当前位置的任何其他方式?还是只能通过它获得?
    • 不,我认为没有其他方法。 getLastKnownLocation 只是为您提供了一种在获得任何更新之前快速获取位置的方法,因此 UI/用户不必等待。但是,我仍然认为您没有获得更新的原因是因为您的参数仅在位置变化超过 100 米时才要求更新。当您将第三个参数更改为 0 时会发生什么?
    • 将两个参数都修改为 0 没有任何作用,并且 getLastKnownLocation 已超过 24 小时。我从 Unity 游戏引擎调用它。所以它显然以某种方式搞砸了这些事件。视图正确打开,只是那些事件没有触发。感谢您对此的帮助。
    • 嗯,鉴于它仍然是 Android Activity,我看不出团结会如何。我的代码几乎没有问题。我看到的唯一真正的区别是我从 onResume 阶段而不是 onCreate 请求位置更新......看不出这有什么关系,但值得快速破解一下。
    猜你喜欢
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    相关资源
    最近更新 更多