【问题标题】:I cannot run application on android, but emulator can我无法在android上运行应用程序,但模拟器可以
【发布时间】:2010-12-27 14:26:24
【问题描述】:
protected void showCurrentLocation(){
 Location lc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
 if(lc != null){
  String msg = String.format("Current Location \n Logitude: %1$s \n Latitude: %2$s",lc.getLongitude(),lc.getLatitude());
  Toast.makeText(Test1.this,msg,3000).show();
 }
 Toast.makeText(Test1.this,"location is null",3000).show();
} 

从上面的代码中,当我在 Android 手机上运行时,它可以运行,但它只显示位置为空。 我不知道为什么它无法从 getLastKnownLocation() 获取位置

http://www.javacodegeeks.com/2010/09/android-location-based-services.html

这是我必须尝试的源代码。请帮我。谢谢卡:))

附言。我已经有了 ACCESS_FINE_LOCATION、ACCESS_MOCK_LOCATION 和 ACCESS_COARSE_LOCATION

【问题讨论】:

    标签: android android-emulator gps


    【解决方案1】:

    您的应用程序是否有使用 GPS 的权限?

    http://developer.android.com/reference/android/Manifest.permission.html

    附注: 这里(http://stackoverflow.com/questions/1608632/android-locationmanager-getlastknownlocation-returns-null)建议如下 - 使用 LocationListener

    private final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
        //TODO:enter your code here
        }
    }
    

    你也可以使用 LocationOverlay:

     final MyLocationOverlay overlay = new MyLocationOverlay(this, mapView);
            overlay.enableMyLocation();
            overlay.runOnFirstFix(new Runnable() {
               public void run() {
                 //TODO: update some model, etc
               }
            });
    

    【讨论】:

    • 我已经有
    • public boolean runOnFirstFix(java.lang.Runnable runnable) 请给我一个上面的例子。
    • 感谢您的回答,但我不明白 private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { //TODO:enter your code here }
    • 好的。您创建新的 Listener 对象。这个对象的目的是倾听。对于某种事件(在这种情况下,位置发生了变化)。当此事件发生(位置更改)时,将调用 onLocationChanged 方法。在'//TODO'部分你必须插入你的源代码,这个部分会在每次位置改变时被调用。
    【解决方案2】:

    Daisy,我认为您正在努力解决的是 getLastKnownLocation() 的概念。在模拟器中,您手动发送模拟位置,并且在您运行代码时确实存在最后一个已知位置。在您的实际手机上,可能还没有最后一个已知位置...

    您可能在想“但我的手机不是总是知道它的位置”,答案可能是,但并非总是如此。

    如上所述,您确实需要编写代码来监听来自位置管理器的位置更新

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      • 2011-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-29
      相关资源
      最近更新 更多