這是我第一次使用服務,它肯定看起來很複雜。使用服務獲取GPS位置,android?
所以我想在用服務關閉我的應用程序之後獲取用戶的位置。
這是我的服務課。
public class LocTrack extends Service {
GPSTracker gp;
@Override
public void onCreate()
{ gp = new GPSTracker(getApplicationContext());
onLocationChanged(gp);
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onLocationChanged(GPSTracker track) {
// Getting latitude
double latitude = track.getLatitude();
// Getting longitude
double longitude = track.getLongitude();
Geocoder geocoder = new Geocoder(LocTrack.this, Locale.getDefault());
try
{
List<Address> addresses = geocoder.getFromLocation(latitude, longitude,1);
Log.e("Addresses","-->"+addresses);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
請告訴我我做錯了什麼。 如果我在活動類中使用代碼,那麼我可以在logcat中獲取地址,但不能在使用服務時獲取地址。
這就是我如何調用從活動服務
Intent intent = new Intent(MainActivity.this, LocTrack.class);
startService(intent);
你有沒有實施清單 –
的權限是 .....LocTrack是我的服務級別的名稱 –
WISHY
否M互聯網,GPS,位置,模擬 –