public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = new DbAdapter(getBaseContext());
db.open();
android_id = Secure.getString(getBaseContext().getContentResolver(), Secure.ANDROID_ID);
locmgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Toast.makeText(getBaseContext(), "Waiting for location..." , Toast.LENGTH_SHORT).show();
}
LocationListener onLocationChange=new LocationListener() {
public void onLocationChanged(Location loc) {
//sets and displays the lat/long when a location is provided
String latlong = "Lat: " + loc.getLatitude() + " Long: " + loc.getLongitude();
Toast.makeText(getBaseContext(), latlong, Toast.LENGTH_SHORT).show();
db.insertGPSCoordinates(android_id, Double.toString(loc.getLatitude()), Double.toString(loc.getLongitude()));
}
public void onProviderDisabled(String provider) {
// required for interface, not used
}
public void onProviderEnabled(String provider) {
// required for interface, not used
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
// required for interface, not used
}
};
//pauses listener while app is inactive
@Override
public void onPause() {
super.onPause();
locmgr.removeUpdates(onLocationChange);
}
//reactivates listener when app is resumed
@Override
public void onResume() {
super.onResume();
locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,10000.0f,onLocationChange);
}
这段代码对我有用,但它只获得Lats and Longs一次。我想知道“LocationListener”是如何工作的?我启动了应用程序并散步,它只存储了1套。我做错了什么......
答案 0 :(得分:0)
您可以将GPS物品放入计时器线程中以强制它更新。不同的设备与GPS监听器的行为不同。
另外,请确保您的insertDBCoordinates不是每次都只是对同一行进行更新
答案 1 :(得分:0)
public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)
当您的位置差异为10000米,10公里时,您的应用程序将仅更新位置更改。试试:
locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,onLocationChange);