-, , requestLocationUpdates(), - , . AT MOST 2 . , ... .
GPS / (, ), ( , , , GPS). , .
, , , 1.5 SDK, , ( 1.1). , GPS / Cupcake, , .
UPDATE:
, , . , SDK 1.1 1.5
( Main.java):
package org.example.LocationTest;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class Main extends Activity implements LocationListener{
private LocationManager myManager;
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.TextView01);
myManager = (LocationManager) getSystemService(LOCATION_SERVICE);
}
@Override
protected void onDestroy() {
stopListening();
super.onDestroy();
}
@Override
protected void onPause() {
stopListening();
super.onPause();
}
@Override
protected void onResume() {
startListening();
super.onResume();
}
private void startListening() {
myManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
this
);
}
private void stopListening() {
if (myManager != null)
myManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
String s = "";
s += "Time: " + location.getTime() + "\n";
s += "\tLatitude: " + location.getLatitude() + "\n";
s += "\tLongitude: " + location.getLongitude() + "\n";
s += "\tAccuracy: " + location.getAccuracy() + "\n";
tv.setText(s);
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
"" ( main.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/TextView01"
android:text="Waiting for location information..."
/>
</LinearLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.LocationTest"
android:versionCode="1"
android:versionName="1.0">
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>
/ . , , - . GPS, - .
.