我目前正在开展一个项目,该项目包含在每 5 秒获取当前位置中。我为此创建了一项服务。但目前的位置没有得到。我试试这个链接。但我没有得到当前的位置。
public class Locfetching extends Service{
LocationManager locationManager;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("2", "2");
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
MyLocationListener myLocationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 1, myLocationListener);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
Log.e("SERVICE123", "LATITUDE:- "+ location.getLatitude() + " Longitude:- " + location.getLongitude());
}
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.e("1", "1");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("3", "3");
if (locationManager != null) {
locationManager = null;
}
}
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.e("SERVICE", "LATITUDE:- "+ location.getLatitude() + " Longitude:- " + location.getLongitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
} }
如何在后台获取当前位置?