2016-10-19 4 views
-3

앱이 매시간 (앱이 실행되지 않을 때) 사용자의 위치를 ​​확인하고 그 시간의 위치가 이전에 기록 된 위치에서 100 마일 이상 떨어져있는 기능을 만들어야합니다. , api를 호출하여 백엔드에서 사용자의 위치를 ​​업데이트하십시오. 이것은 물론 배터리/전력 소비를 최소화하기 위해 고려됩니다.매시간 백그라운드에서 위치 확인

저는 계속해서 운영되는 서비스로 일한 적이 없습니다. 이것을 달성하기위한 최선의 방법은 무엇입니까?

+0

을 나는 당신이 읽거나 새로운 것을 시도하지 돈 생각 .................. ....이 경우 위치 관리자에 대해 묻지 마십시오.이 코드의 한 줄은 "LocationManager.requestLocationUpdates (LocationManager.GPS_PROVIDER, GPS_TIME_INTERVAL, GPS_DISTANCE, GPSListener);" –

+0

@GaneshPokale 위치 관리자와 함께이 앱의 다른 부분에 대한 사용자 위치 정보를 얻었습니다. 내가 정말로 묻는 부분은 앱이 실행 중이 지 않을 때마다이를 수행하는 방법입니다. – yellowmonkey

+0

장치에서 백그라운드로 실행되는 서비스를 생성하고 위치 관리자에게 전화하여 해당 서비스에서 1 시간 동안 GPS_TIME_INTERVAL 집합을 업데이트하도록 요청합니다. 나는 약간의 흐름과 개념을 이해한다. 나는이 질문을 통해 앱이 실행되지 않을 때 배경 서비스로 일한 적이 없기 때문에 최선의 접근법이 무엇인지 배울 것을 요청한다. – yellowmonkey

답변

2
public class MyService extends Service 
{ 
private static final String TAG = "BOOMBOOMTESTGPS"; 
private LocationManager mLocationManager = null; 
private static final int LOCATION_INTERVAL = 1000 * 60 * 60; // 1 hour 
private static final float LOCATION_DISTANCE = 160935f; // 100 miles 

private class LocationListener implements android.location.LocationListener 
    { 
    Location mLastLocation; 

    public LocationListener(String provider) 
    { 
     Log.e(TAG, "LocationListener " + provider); 
     mLastLocation = new Location(provider); 
    } 

    @Override 
    public void onLocationChanged(Location location) 
    { 
     Log.e(TAG, "onLocationChanged: " + location); 
     mLastLocation.set(location); 
    } 

    @Override 
    public void onProviderDisabled(String provider) 
    { 
     Log.e(TAG, "onProviderDisabled: " + provider); 
    } 

    @Override 
    public void onProviderEnabled(String provider) 
    { 
     Log.e(TAG, "onProviderEnabled: " + provider); 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) 
    { 
     Log.e(TAG, "onStatusChanged: " + provider); 
    } 
} 

LocationListener[] mLocationListeners = new LocationListener[] { 
     new LocationListener(LocationManager.GPS_PROVIDER), 
     new LocationListener(LocationManager.NETWORK_PROVIDER) 
}; 

@Override 
public IBinder onBind(Intent arg0) 
{ 
    return null; 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) 
{ 
    Log.e(TAG, "onStartCommand"); 
    super.onStartCommand(intent, flags, startId); 
    return START_STICKY; 
} 

@Override 
public void onCreate() 
{ 
    Log.e(TAG, "onCreate"); 
    initializeLocationManager(); 
    try { 
     mLocationManager.requestLocationUpdates(
       LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, 
       mLocationListeners[1]); 
    } catch (java.lang.SecurityException ex) { 
     Log.i(TAG, "fail to request location update, ignore", ex); 
    } catch (IllegalArgumentException ex) { 
     Log.d(TAG, "network provider does not exist, " + ex.getMessage()); 
    } 
    try { 
     mLocationManager.requestLocationUpdates(
       LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, 
       mLocationListeners[0]); 
    } catch (java.lang.SecurityException ex) { 
     Log.i(TAG, "fail to request location update, ignore", ex); 
    } catch (IllegalArgumentException ex) { 
     Log.d(TAG, "gps provider does not exist " + ex.getMessage()); 
    } 
} 

@Override 
public void onDestroy() 
{ 
    Log.e(TAG, "onDestroy"); 
    super.onDestroy(); 
    if (mLocationManager != null) { 
     for (int i = 0; i < mLocationListeners.length; i++) { 
      try { 
       mLocationManager.removeUpdates(mLocationListeners[i]); 
      } catch (Exception ex) { 
       Log.i(TAG, "fail to remove location listners, ignore", ex); 
      } 
     } 
    } 
} 

private void initializeLocationManager() { 
    Log.e(TAG, "initializeLocationManager"); 
    if (mLocationManager == null) { 
     mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE); 
    } 
} 
} 

귀하의 AndroidManifest.xml는 다음과 같이 표시됩니다

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity android:label="@string/app_name" android:name=".LocationCheckerActivity" > 
     <intent-filter > 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <service android:name=".MyService" android:process=":my_service" /> 
</application> 
+0

나는 모든 코드를 제공하는 사람을 정말로 기대하지 않았고, 최선의 접근법을 배우기를 희망했다. 이에. 어쨌든 Ganesh, 매우 도움이됩니다. – yellowmonkey

+0

해피 코딩 :) –

+0

이 위치 관리자는 항상 실행 중이십니까? 이 접근법은 어떻습니까? 에서 onCreate 내부 – yellowmonkey