Я написал следующий код для получения названия места:
UseGpsActivity.java
public class UseGpsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the LocationManager class to obtain GPS locations */
boolean gps_is_enabled = false;
LocationManager mlocManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
List<String> accessibleProviders = mlocManager.getProviders(true);
gps_is_enabled = accessibleProviders != null
&& accessibleProviders.size() > 0;
Log.v("gps enabled", gps_is_enabled + "");
LocationListener mlocListener = new MyLocationListener();
// mlocManager.requestLocationUpdates(mlocManager.GPS_PROVIDER, 0, 0,
// mlocListener);
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
mlocListener);
// mlocManager.set
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener {
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Disabled", 5000)
.show();
Log.v("gps", "gps disabled");
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Enabled", 5000).show();
Log.v("gps", "gps enabled");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " + "Latitud = "
+ loc.getLatitude() + "Longitud = " + loc.getLongitude();
Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT)
.show();
Geocoder gcd = new Geocoder(UseGpsActivity.this,
Locale.getDefault());
List<Address> addresses = null;
try {
addresses = gcd.getFromLocation(loc.getLatitude(),
loc.getLongitude(), 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (addresses != null) {
if (addresses.size() > 0) {
Log.v("Addresses : ", addresses.get(0).getLocality() + "");
Toast.makeText(
getApplicationContext(),
"Addresses : " + addresses.get(0).getLocality()
+ "", Toast.LENGTH_LONG).show();
Log.v("gps", Text + " " + addresses.get(0).getLocality()
+ "");
}
} else {
Log.v("addresses ", "NULL");
}
}
}/* End of Class MyLocationListener */
Код на Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="demo.usegps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.SET_TIME_ZONE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".UseGpsActivity"
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>
</manifest>
Когда я использую geo fix (geo fix -122.084094 37.422006
), я вижу тост с широтой и долготой, но на logcat я получаю следующее сообщение:
01-19 13:32:48.991: W/System.err(751): java.io.IOException: Service not Available
01-19 13:32:48.991: W/System.err(751): at android.location.Geocoder.getFromLocation(Geocoder.java:117)
01-19 13:32:49.001: W/System.err(751): at demo.usegps.UseGpsActivity$MyLocationListener.onLocationChanged(UseGpsActivity.java:74)
01-19 13:32:49.001: W/System.err(751): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:191)
01-19 13:32:49.001: W/System.err(751): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:124)
01-19 13:32:49.001: W/System.err(751): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:140)
01-19 13:32:49.001: W/System.err(751): at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 13:32:49.001: W/System.err(751): at android.os.Looper.loop(Looper.java:123)
01-19 13:32:49.001: W/System.err(751): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-19 13:32:49.001: W/System.err(751): at java.lang.reflect.Method.invokeNative(Native Method)
01-19 13:32:49.001: W/System.err(751): at java.lang.reflect.Method.invoke(Method.java:521)
01-19 13:32:49.001: W/System.err(751): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-19 13:32:49.011: W/System.err(751): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-19 13:32:49.011: W/System.err(751): at dalvik.system.NativeStart.main(Native Method)
01-19 13:32:49.011: V/addresses(751): NULL
Какой сервис мне нужно добавить или позвонить в моем коде ??