【问题标题】:Location changed notification android位置更改通知android
【发布时间】:2012-01-06 15:41:22
【问题描述】:

我只是在 Android 中测试 onLocationChanged 方法,我已经实现了一种方法,当我单击按钮时,应用程序会返回一个带有新 lat 和 lng 值的字符串。但现在我试图在每次位置更改时自动显示结果。这就是我目前要做的:

location manager

public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);   

int minTime = 30000;
LocationManager locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
MyLocationListener myLocListener = new MyLocationListener();
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setSpeedRequired(false);
String bestProvider = locationManager.getBestProvider(criteria, false);

locationManager.requestLocationUpdates(bestProvider, minTime, 1, myLocListener );

位置改变了

public void onLocationChanged(Location loc) {

  if(loc != null){

  lat1 = loc.getLatitude();
  lng1 = loc.getLongitude();
  
  currentlocation = "Location changed to lat: " + lat + "" + "lng: " + lng1 ;

    Context context = getApplicationContext();
    CharSequence text = currentlocation;
    int duration = Toast.LENGTH_LONG;

    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
   
  
  }

}

我已将 requestlocationupdates 方法设置为每 60000 毫秒请求和更新一次 我的问题是,当位置发生变化时,应用程序永远不会烘烤 lat 和 lng 值。我已经使用模拟器测试了该应用程序并尝试远程登录 lat 和 lng 值。而且我还开着我的车到处转转,看看这些值是否发生了变化。

【问题讨论】:

    标签: android gps location


    【解决方案1】:

    试试:

    currentlocation = "Location changed to lat: " + String.valueOf(lat1) + "" + "lng: " + String.valueOf(lng1) ;
    

    对于测试,您可以使用:

    requestLocationUpdates(LocationManager.GPS_PROVIDER... 
    

    【讨论】:

    • 谢天谢地,它工作得很好,我必须使用 .GPS_PROVIDER 还是我可以使用设备可以找到的最佳提供商?再次欢呼
    • 你可以使用你的,我使用 GPS_Provider,来检查它是否真的有效。如果有帮助,您能否“提高”我的答案:)
    猜你喜欢
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多