我正在尝试创建一个Android服务,每隔5分钟就会对用户的位置进行更新。我正在使用DDMS将坐标发送到模拟器,工作正常。我需要转换这些坐标并获取位置Eg:纽约并使用Toast,在屏幕上打印。我没有使用任何地图我试图使用地理编码器将DDMS提供的坐标转换为该位置。我没有得到错误,但似乎坐标没有转换到该位置,屏幕上没有显示任何内容。请帮忙一直在努力解决这个问题很长一段时间。这是我的代码。谢谢
public class GetLocationService extends Service {
protected LocationManager locationManager;
Button start;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
LocationListener ll = new MyLocListener();
Location location = new Location("abc");
ll.onLocationChanged(location );
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, ll);
return START_STICKY;
}
private class MyLocListener implements LocationListener {
public void onLocationChanged(Location location) {
if (location != null) {
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
}
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Toast.makeText(getApplicationContext(),
location.getLatitude() + "" + location.getLongitude(),
Toast.LENGTH_LONG).show();
try{
Geocoder geo = new Geocoder(GetLocationService.this.getApplicationContext(), Locale.getDefault());
List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
Log.d("TAG",addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +"," + addresses.get(0).getAdminArea() + "," + addresses.get(0).getCountryName());
}
Toast.makeText(getApplicationContext(),
addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +"," + addresses.get(0).getAdminArea() + "," + addresses.get(0).getCountryName(),
Toast.LENGTH_LONG).show();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
////开始服务////
public class MyService extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.service);
startService(new Intent(getBaseContext(), GetLocationService.class));
}
}
答案 0 :(得分:0)
好吧,你的日志声明也会通过吗?此外,您的toast将不会显示,因为您传递的上下文是服务上下文..该服务不是用户界面的一部分..因此不显示服务上下文。应用程序上下文可能有用...但你真的应该使用当前活动的上下文。