【问题标题】:Android - Unable to get the gps location on the emulatorAndroid - 无法在模拟器上获取 gps 位置
【发布时间】:2011-01-20 09:56:56
【问题描述】:

我正在尝试在 android 模拟器上使用 gps,我有以下代码:

public class NL extends Activity {

 private LocationManager locmgr = null;

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nl);

        locmgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        Criteria crit = new Criteria();
        crit.setAccuracy(Criteria.ACCURACY_FINE);
        String provider = locmgr.getBestProvider(crit, true);
        Location loc = locmgr.getLastKnownLocation(provider);


        Toast msg = Toast.makeText(this, "Lon: " + Double.toString(loc.getLongitude()) + " Lat: " + Double.toString(loc.getLatitude()), Toast.LENGTH_SHORT);
        msg.show();
    }
}

我在清单中添加了以下行:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

我已经使用 DDMS 方法和 geo fix 方法设置了 gps 位置,但是当我运行代码时,我在 Toast 行得到一个 NullPointerExeption,可能是因为 loc 为空。

我不明白错误出在哪里...你能帮帮我吗?


更新!

感谢您的帮助,现在我使用以下代码并且没有收到任何错误,但它没有运行 onChangeLocation 内的代码...它没有运行 Toast 并且不返回任何消息在日志中!

 public class NL extends Activity {

        private LocationManager locmgr = null;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.nl);

            locmgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

         // Define a listener that responds to location updates
            LocationListener locationListener = new LocationListener() {
                public void onLocationChanged(Location loc) {
                  // Called when a new location is found by the network location provider.
                    Log.i("NOTIFICATION","onLocationChenged Triggered");

                    Toast msg = Toast.makeText(NetworkLocator.this, "Lon: " + Double.toString(loc.getLongitude()) + " Lat: " + Double.toString(loc.getLatitude()), Toast.LENGTH_SHORT);
                    msg.show();
                }

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

                public void onProviderEnabled(String provider) {}

                public void onProviderDisabled(String provider) {}
              };

            // Register the listener with the Location Manager to receive location updates
            locmgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        }
    }

谢谢!

【问题讨论】:

  • 您是否验证过 locmgr.getBestProvider 是否真的返回“gps”?无论如何,您绝对应该实现一个 LocationListener 并使用 requestLocationUpdates() 将其传递给 LocationManager
  • getBestProvider 返回“gps”。即使我尝试使用 lastKnownLocation 获取位置,我是否也要添加位置侦听器?感谢您的帮助
  • @Marco Faion 请及时更新您的答案...我已经回答过您一次,但您没有对答案提供任何反馈! > http://stackoverflow.com/questions/4591765/android-sqlite-database-method-undefined-fot-type/4591843#4591843
  • 你好宏你得到解决方案了吗?
  • 将日志输出到onLocationChange 并在 Logcat 中查看是否有打印内容...

标签: java android android-emulator gps


【解决方案1】:

模拟器一开始没有任何位置。根据doc,'getLastKnownLocation'方法可以返回null,所以没问题。在这种情况下,您应该等待位置更新(您可以使用 LocationManager 中的 requestLocationUpdates 方法)。您可以通过以下命令在模拟器的 gps 模块上触发位置更新:

adb -e emu geo fix 50 50

【讨论】:

    【解决方案2】:

    找到解决方案:

    LocationManager.NETWORK_PROVIDER 错误。

    更正:LocationManager.GPS_PROVIDER

    【讨论】:

    • 根据我的经验,当提供程序设置为 NETWORK_PROVIDER 时,模拟器似乎不会响应通过 DDMS 发送的位置更改,但正如您所指出的,当它设置为 GPS_PROVIDER 时会响应。对我来说,这看起来像是模拟器中的一个限制,或者至少是一种不明显的行为。
    【解决方案3】:

    如果你所描述的一切都完成了,那么你可能没有在模拟器中打开你的 gps。转到设置:->位置和安全性:->应该检查使用 gps 卫星


    已编辑:我认为您必须使用没有标准类型的位置管理器。



    LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    
    loc=mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    

    ------而不是尝试变长。和纬度。

    【讨论】:

    • 勾选“使用gps卫星”
    【解决方案4】:

    您确定您的模拟设备确实支持 GPS? 我认为这有一个额外的选择......

    这也可能有帮助:

    谢谢!对所有人:您可以随时在 Eclipse 中更改项目的构建目标:在 Package Explorer 中右键单击项目,选择 Properties > Android,然后检查 Project Target 的“Google API”。 -- 使用非英语文化设置的开发人员可能会注意到,按下位置控制中的发送按钮不会向 Android 模拟器发送新位置。它已在即将发布的 SDK 工具第 7 版中修复;为了快速修复,您可以将您的语言环境更改为“en”。 (有关详细信息,请参阅 code.google.com/p/android/issues/detail?id=915。)– Patrick de Kleijn

    来源:GPS on emulator doesn't get the geo fix - Android

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-08
      • 1970-01-01
      • 2016-09-24
      • 1970-01-01
      相关资源
      最近更新 更多