アクティビティを呼び出して現在の gsp の位置を取得し、経度と緯度を返したいと思っていましたが、何も起こらなかったため、プログラミングの本から次の簡単なコードを試しました。
{private static final String TAG = GetGps.class.getSimpleName();
private LocationManager manager;
private LocationListener listener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gps);
// LocationManager-Instanz ermitteln
manager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Liste mit Namen aller Provider erfragen
List<String> providers = manager.getAllProviders();
// Infos zu Location Providern ausgeben
for (String name : providers) {
LocationProvider lp = manager.getProvider(name);
Log.d(TAG,
lp.getName() + " --- isProviderEnabled(): "
+ manager.isProviderEnabled(name));
Log.d(TAG, "requiresCell(): " + lp.requiresCell());
Log.d(TAG, "requiresNetwork(): " + lp.requiresNetwork());
Log.d(TAG, "requiresSatellite(): " + lp.requiresSatellite());
}
// Provider mit grober Auflösung
// und niedrigen Energieverbrauch
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
// Namen ausgeben
String name = manager.getBestProvider(criteria, true);
Log.d(TAG, name);
// LocationListener-Objekt erzeugen
listener = new LocationListener() {
public void onStatusChanged(String provider, int status,
Bundle extras) {
Log.d(TAG, "onStatusChanged()");
}
public void onProviderEnabled(String provider) {
Log.d(TAG, "onProviderEnabled()");
}
public void onProviderDisabled(String provider) {
Log.d(TAG, "onProviderDisabled()");
}
public void onLocationChanged(Location location) {
Log.d(TAG, "onLocationChanged()");
if (location != null) {
String s = "Breite: " + location.getLatitude()
+ "\nLänge: " + location.getLongitude();
Toast.makeText(GetGps.this,
s,
Toast.LENGTH_LONG).show();
}
}
};
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart()");
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0,
listener);
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause()");
manager.removeUpdates(listener);
}}
私が言ったように、何も起こらないので、ここにカタログがあります:
12-05 15:59:54.490: D/GetGps(6833): network --- isProviderEnabled(): true
12-05 15:59:54.490: D/GetGps(6833): requiresCell(): true
12-05 15:59:54.490: D/GetGps(6833): requiresNetwork(): true
12-05 15:59:54.490: D/GetGps(6833): requiresSatellite(): false
12-05 15:59:54.500: D/GetGps(6833): passive --- isProviderEnabled(): true
12-05 15:59:54.500: D/GetGps(6833): requiresCell(): false
12-05 15:59:54.500: D/GetGps(6833): requiresNetwork(): false
12-05 15:59:54.500: D/GetGps(6833): requiresSatellite(): false
12-05 15:59:54.500: D/GetGps(6833): gps --- isProviderEnabled(): true
12-05 15:59:54.500: D/GetGps(6833): requiresCell(): false
12-05 15:59:54.500: D/GetGps(6833): requiresNetwork(): true
12-05 15:59:54.500: D/GetGps(6833): requiresSatellite(): true
12-05 15:59:54.500: D/GetGps(6833): network
12-05 15:59:54.500: D/GetGps(6833): onStart()
12-05 15:59:54.530: D/OpenGLRenderer(6833): Flushing caches (mode 0)
誰かが私を助けてくれますか??