Android - 使用 LocationManager 不提供地理修复

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/938719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:31:51  来源:igfitidea点击:

Android - using LocationManager does not give a geo fix

androidgps

提问by lostInTransit

I am trying to get the GPS location of my G1 using the following code

我正在尝试使用以下代码获取 G1 的 GPS 位置

In Activity

活动中

MyLocationListener myListener = new MyLocationListener();
LocationManager myManager = (LocationManager)getSystemService(LOCATION_SERVICE);
myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, myListener);

This is the LocationListener class

这是 LocationListener 类

public class MyLocationListener implements LocationListener {

  private static double latitude;
  private static double longitude;

  @Override
  public void onLocationChanged(Location arg0) {
    latitude = arg0.getLatitude();
    longitude = arg0.getLongitude();
  }

  @Override
  public void onProviderDisabled(String provider) {
  }

  @Override
  public void onProviderEnabled(String provider) {
  }

  @Override
  public void onStatusChanged(String provider, int status, Bundle extras) {}

  public static double getLatitude() {
    return latitude;
  }

  public static double getLongitude() {
    return longitude;
  }

}

I waited for as long as 30 seconds but none of the listener methods were called. The GPS icon shows up, stays there for some time but for some reason I don't get a fix, even outdoors in bright sunlight. I am testing on a G1 with 1.5 SDK.

我等了 30 秒,但没有调用任何侦听器方法。GPS 图标出现,在那里停留了一段时间,但由于某种原因我没有得到修复,即使在户外明亮的阳光下。我正在使用 1.5 SDK 对 G1 进行测试。

Can someone please tell me what's wrong with the code? Thanks.

有人可以告诉我代码有什么问题吗?谢谢。

Adding log

添加日志

06-02 18:30:43.143: ERROR/System(52): java.lang.SecurityException
06-02 18:30:43.143: ERROR/System(52):     at android.os.BinderProxy.transact(Native Method)
06-02 18:30:43.143: ERROR/System(52):     at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
06-02 18:30:43.143: ERROR/System(52):     at android.os.ServiceManager.addService(ServiceManager.java:72)
06-02 18:30:43.143: ERROR/System(52):     at com.android.server.ServerThread.run(SystemServer.java:155)
06-02 18:30:43.152: ERROR/AndroidRuntime(52): Crash logging skipped, no checkin service

06-02 18:30:43.382: ERROR/SystemServer(52): Failure starting StatusBarService
06-02 18:30:43.382: ERROR/SystemServer(52): java.lang.NullPointerException
06-02 18:30:43.382: ERROR/SystemServer(52):     at com.android.server.status.StatusBarPolicy.updateBluetooth(StatusBarPolicy.java:749)
06-02 18:30:43.382: ERROR/SystemServer(52):     at com.android.server.status.StatusBarPolicy.<init>(StatusBarPolicy.java:282)
06-02 18:30:43.382: ERROR/SystemServer(52):     at com.android.server.status.StatusBarPolicy.installIcons(StatusBarPolicy.java:337)
06-02 18:30:43.382: ERROR/SystemServer(52):     at com.android.server.ServerThread.run(SystemServer.java:186)
06-02 18:30:43.382: ERROR/AndroidRuntime(52): Crash logging skipped, no checkin service

回答by Jeremy Logan

Firstly, in your activity, where you're calling requestLocationUpdates(), the second argument is the minimum time, not the scheduled time. You're currently asking for an update AT MOST every 2 seconds. It won't actually report anything until it can... sometimes it takes a little while.

首先,在您调用的活动中,requestLocationUpdates()第二个参数是最短时间,而不是预定时间。您目前最多每 2 秒要求更新一次。它实际上不会报告任何东西,直到它可以......有时需要一段时间。

If your GPS is unresponsive/slow elsewhere as well (like in Maps) try restarting the phone (some people claim you have to turn off the phone then pull out the battery to completely de-energized the GPS radio). Hopefully this will bring it back up to it's normally responsive state.

如果您的 GPS 在其他地方也没有响应/缓慢(例如在地图中),请尝试重新启动手机(有些人声称您必须关闭手机然后拔出电池才能将 GPS 无线电完全断电)。希望这将使其恢复到通常的响应状态。

Also, it shouldn't matter, but you said you were using the 1.5 SDK, but you didn't mention if you were compiling against it (as opposed to 1.1). I only bring this up because the GPS works better/faster in Cupcake, so I was wondering what your phone was actually running.

此外,这应该无关紧要,但您说您使用的是 1.5 SDK,但您没有提及您是否针对它进行编译(而不是 1.1)。我提出这个只是因为 GPS 在 Cupcake 中工作得更好/更快,所以我想知道你的手机实际运行的是什么。

UPDATE:

更新:

I wrote up a little test on my own to see if I could duplicate your problem. It consists of just three user-generated files and I tested it with SDK 1.1 and 1.5

我自己写了一个小测试,看看我是否可以复制你的问题。它仅包含三个用户生成的文件,我使用 SDK 1.1 和 1.5 对其进行了测试

In the main Activity (I simply used Main.java):

在主活动中(我只是使用了Main.java):

package org.example.LocationTest;

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class Main extends Activity implements LocationListener{
    private LocationManager myManager;
    private TextView tv;


    /********************************************************************** 
     * Activity overrides below 
     **********************************************************************/
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // get a handle to our TextView so we can write to it later
        tv = (TextView) findViewById(R.id.TextView01);

        // set up the LocationManager
        myManager = (LocationManager) getSystemService(LOCATION_SERVICE);   
    }

    @Override
    protected void onDestroy() {
        stopListening();
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        stopListening();
        super.onPause();
    }

    @Override
    protected void onResume() {
        startListening();
        super.onResume();
    }



    /**********************************************************************
     * helpers for starting/stopping monitoring of GPS changes below 
     **********************************************************************/
    private void startListening() {
        myManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            0, 
            0, 
            this
        );
    }

    private void stopListening() {
        if (myManager != null)
            myManager.removeUpdates(this);
    }




    /**********************************************************************
     * LocationListener overrides below 
     **********************************************************************/
    @Override
    public void onLocationChanged(Location location) {
        // we got new location info. lets display it in the textview
        String s = "";
        s += "Time: "        + location.getTime() + "\n";
        s += "\tLatitude:  " + location.getLatitude()  + "\n";
        s += "\tLongitude: " + location.getLongitude() + "\n";
        s += "\tAccuracy:  " + location.getAccuracy()  + "\n";

        tv.setText(s);
    }    

    @Override
    public void onProviderDisabled(String provider) {}    

    @Override
    public void onProviderEnabled(String provider) {}    

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}
}

In your "main" layout file (I used main.xml):

在您的“主”布局文件中(我使用过main.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:id="@+id/TextView01"
        android:text="Waiting for location information..." 
    />
</LinearLayout>

Then, in your AndroidManifest.xml:

然后,在您的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.example.LocationTest"
    android:versionCode="1"
    android:versionName="1.0">
    <application 
        android:icon="@drawable/icon" 
        android:label="@string/app_name">
        <activity 
            android:name=".Main"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="3" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest> 

You should be able to compile/run this just fine. If, using this, you still can't get a fix it's not a software problem. You either have a bad GPS or it just can't get a lock on any satellites.

您应该能够很好地编译/运行它。如果使用此方法仍然无法修复,则不是软件问题。您要么有一个糟糕的 GPS,要么就是无法锁定任何卫星。

You can download the source here.

你可以在这里下载源代码。

回答by CommonsWare

  1. Is your GPS enabled in your device settings? I presume so since you are getting the GPS icon, but I figured I'd check.
  2. onProviderDisabled() will only get called if you disable GPS in the settings, and so that code is unlikely to get invoked.
  3. Have you tried a distance of 0 instead of 2000?
  4. Are you sure that your call to requestLocationUpdates() is actually getting executed?
  5. Does your log show any errors? You can examine your log via adb logcat, DDMS, or the DDMS perspective in Eclipse.
  6. Have you tried any existing code, published, that should work? For example, you can grab the source code to my Android book, where the Weather and WeatherPlus samples show the use of LocationManager.
  1. 您的设备设置中是否启用了 GPS?我想是因为你得到了 GPS 图标,但我想我会检查一下。
  2. 只有在设置中禁用 GPS 时才会调用 onProviderDisabled(),因此不太可能调用该代码。
  3. 您是否尝试过距离为 0 而不是 2000?
  4. 您确定您对 requestLocationUpdates() 的调用实际上正在执行吗?
  5. 您的日志是否显示任何错误?您可以通过 adb logcat、DDMS 或 Eclipse 中的 DDMS 透视图来检查您的日志。
  6. 您是否尝试过任何现有的代码,已发布,应该可以工作?例如,您可以获取我的 Android book的源代码,其中 Weather 和 WeatherPlus 示例展示了 LocationManager 的使用。

回答by Fedor

Here's a nice example with source code http://marakana.com/forums/android/android_examples/42.html

这是一个带有源代码的很好的示例http://marakana.com/forums/android/android_examples/42.html