내 프로젝트에서 백그라운드로 계속 위치를 검색하는 서비스를 사용했습니다.백그라운드에서 계속 현재 위치 얻기
public class LocationService extends Service
{
private static final int TWO_MINUTES = 1000 * 60 * 2;
public LocationManager locationManager;
public MyLocationListener listener;
public Location previousBestLocation = null;
Intent i;
@Override
public void onCreate()
{
super.onCreate();
i = new Intent("LOCATION_CHANGED");
}
@Override
public int onStartCommand(Intent intent,int flags, int startId)
{
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener);
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;
}
// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;
// If it's been more than two minutes since the current location, use the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must be worse
} else if (isSignificantlyOlder) {
return false;
}
// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
return true;
}
return false;
}
/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
@Override
public void onDestroy() {
// handler.removeCallbacks(sendUpdatesToUI);
super.onDestroy();
Log.v("STOP_SERVICE", "DONE");
locationManager.removeUpdates(listener);
}
public static Thread performOnBackgroundThread(final Runnable runnable) {
final Thread t = new Thread() {
@Override
public void run() {
try {
runnable.run();
} catch(Exception e) {
System.out.print(e);
}
}
};
t.start();
return t;
}
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(final Location loc)
{
Log.i("**************************************", "Location changed");
if(isBetterLocation(loc, previousBestLocation)) {
String newLocation = "Lat: " + loc.getLatitude() + " and Long: " + loc.getLongitude();
i = new Intent("LOCATION_CHANGED");
i.putExtra("location", newLocation);
sendBroadcast(i);
}
}
public void onProviderDisabled(String provider)
{
Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show();
}
public void onProviderEnabled(String provider)
{
Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
}
을 다음과 같이 내가 MainActivity
의 의도를 방송 수신 한 :
public class MainActivity extends Activity {
TextView txt_location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt_location = (TextView)findViewById(R.id.current_location);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public class LocationBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context ctx, Intent intent) {
txt_location.setText(intent.getExtras().getString("location"));
}
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container,
false);
}
}
}
내가하고 싶은 모든 txt_location
에 위치의 위도와 경도를 설정하는 것입니다 다음과 같이 내 LocationService
이다. 이 코드가 작동하고 그대로 txt_location
표시되지 않습니다 : 당신이 그것을 어떤 위치 업데이트를 표시되지 볼 수 있듯이
... 로그 캣은 어떤 errors..Can의 사람이 표시되지 안내하시기 바랍니다 나 무슨 실수 야? 서비스를 시작하는
사용 AndoridHive 자습서 : http://www.androidhive.info/2012/07/android -Gps-location-manager-tutorial/ –
@TusharPandey 일부 버튼 클릭으로 위치를 검색합니다. 나는 (서비스 또는 다른 방법을 사용하여) 백그라운드에서 계속적으로 위치를 얻고 싶다고 말했다. – xyz
서비스를 만들거나 무제한 시간 동안 백그라운드 스레드를 사용하고 위치가 변경되면 localbroadcast msg를 생성한다. –