Android 휴대 전화에서 위치 추적 애플리케이션을 만들려고합니다. 휴대 전화에 내장 된 Google지도와 동일합니다. 앱이 종료 된 후에도 계속해서 휴대 전화를 추적하므로 다른 애플리케이션의 백그라운드에서 실행됩니다.위치 수신기와 Google지도를 분리하는 방법
사용자 인터페이스 (Google지도)와 위치 추적을 모두 분리하려고합니다. 위치 추적은 항상 실행되지만 Google지도는 사용자가 응용 프로그램을 열 때만 표시됩니다. 따라서 이것은 지금까지 제가 도달 한 것입니다. 구글지도에 대한
MapDisplay 클래스 I 때문에이 라인
myLocation = locationService.getMyLocation();
String.valueOf(myLocation.getLatitude())
의 응용 프로그램 충돌을 경험 한 위의 코드를 사용하여
public class MapDisplay extends Activity {
private GoogleMap map;
private LocationService locationService;
private Intent locationServiceIntent;
private Location myLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_display);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.googleMap)).getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.setMyLocationEnabled(true);
locationServiceIntent = new Intent(this, LocationService.class);
x=0;
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
startService(locationServiceIntent);
bindService(locationServiceIntent, locationServiceConnection, Context.BIND_AUTO_CREATE);
}
@Override
public void updateLocation()
{
// for testing purpose, to make sure it will update periodically
Toast.makeText(MapDisplay.this, String.valueOf(myLocation.getLatitude()), Toast.LENGTH_SHORT).show();
}
private ServiceConnection locationServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
locationService = ((LocationService.LocationBinder)service).getService();
myLocation = locationService.getMyLocation();
updateLocation();
}
@Override
public void onServiceDisconnected(ComponentName name) {
locationService = null;
}
};
}
LocationService 클래스
public class LocationService extends Service implements LocationListener
{
private LocationManager locationManager;
private Location myLocation;
private IBinder locationBinder = new LocationBinder();
public class LocationBinder extends Binder
{
public LocationService getService()
{
return LocationService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return locationBinder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
myLocation = null;
boolean networkProviderAvailable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
boolean gpsProviderAvailable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(networkProviderAvailable)
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
else if(gpsProviderAvailable)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
else
{
stopSelf();
}
return super.onStartCommand(intent, flags, startId);
}
public Location getMyLocation()
{
return myLocation;
}
@Override
public void onLocationChanged(Location location) {
myLocation = location;
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
이 위치를 얻을 수 없다 LocationService 클래스에서.
그래서, 내 질문은 :
- 1)이 응용 프로그램을 만들 수있는 좋은 방법입니다?
- 2) onLocationChanged 메소드에 새 업데이트가있을 때 MapDisplay에서 updateLocation()을 주기적으로 호출하는 방법은 무엇입니까?
나는 누군가가 나를 도울 수 있다면 크게 감사하겠습니다.
멋지다, 고마워. – user3833542