2012-01-06 126 views
3

嘿家伙我只是测试了Android中的onLocationChanged方法,我实现了一种方式,当我单击按钮时,应用程序返回一个带有新的lat和lng值的String。但是现在我试图在每次位置变化时自动显示结果。这是我迄今为止这样做:位置更改通知android

位置管理

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); 

onlocationchanged

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个millseconds 我的问题是,应用程序永远不会为位置更改时的lat和lng值生成敬酒。我已经使用模拟器测试了应用程序,并尝试通过lat和lng值。我也开着车在车里看看价值观是否改变。

回答

2

尝试:

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

和测试,你可以只使用:

requestLocationUpdates(LocationManager.GPS_PROVIDER... 
+0

谢谢goodm它的工作完美,我一定要使用.GPS_PROVIDER或者我可以用最好的供应商的设备可以找 ?再次欢呼 – 2012-01-06 17:26:03

+0

你可以使用你的一个,我使用GPS_Provider来检查它是否真的有效。如果它有帮助,你能“上”我的回答:) – goodm 2012-01-07 11:41:43