2014-10-05 35 views
0

GPS位置更新我全新到Android,iOS版從遷移。我正在開發一款健身應用程序(4.3+),用於計算用戶的位置並對其進行跟蹤。一切都很好,除非應用程序進入後臺,我停止接收位置更新。接收背景

我目前使用的從我的GPSManager類的UserFitness類廣播接收器。在GPSManager類中,我啓動了LocationManager並啓動了其他一切,包括開始位置更新。然後,每當有新的位置更新可用且某些計算在此完成,存儲並顯示給用戶時,它就會廣播到UserFitness類。我研究過需要爲LocationManager實現一個服務以在後臺工作,但我不確定如何爲當前代碼實現它。我的所有代碼都在下面。

誰能告訴我如何根據我的代碼來獲得在後臺位置更新,這是否可進行約4小時時間沒有系統破壞的過程?如果系統確實銷燬了它,那麼可以採取什麼措施使流程儘快恢復到之前的狀態?

謝謝!


CODE

GPSManager.java

public class GPSManager{ 

private static final String TAG = "GPSManager"; 
public static final String ACTION_LOCATION = "com.fitnesstracker.ACTION_LOCATION"; 

private static GPSManager sGPSManager; 
private Context mAppContext; 
private LocationManager mLocationManager; 
private Location mLastLocation; 

private GPSManager (Context appContext,GpsStatus.Listener listen) 
{ 
    mAppContext = appContext; 
    mLocationManager = (LocationManager) mAppContext.getSystemService(Context.LOCATION_SERVICE); 
    mLocationManager.addGpsStatusListener(listen); 
} 

public static GPSManager get (Context c,GpsStatus.Listener listen) 
{ 
    if (sGPSManager == null) 
    { 
     //USE THE APPLICATION CONTEXT TO AVOID LEAKING ACTIVITIES 
     sGPSManager = new GPSManager(c,listen); 
    } 
    return sGPSManager; 
} 

private PendingIntent getLocationPendingIntent (boolean shouldCreate) 
{ 
    Intent broadcast = new Intent(ACTION_LOCATION); 
    int flags = shouldCreate ? 0 : PendingIntent.FLAG_NO_CREATE; 

    return PendingIntent.getBroadcast(mAppContext, 0, broadcast, flags); 
} 

public void startLocationUpdates() 
{ 
    String provider = LocationManager.GPS_PROVIDER; 

    //GET LAST KNOWN LOCATION AND BROADCAST IF THERE IS ONE 

    mLastLocation = mLocationManager.getLastKnownLocation(provider); 
    if (mLastLocation != null) 
    { 
     mLastLocation.setTime(System.currentTimeMillis()); 
     broadcastLocation(mLastLocation); 
    } 

    //START UPDATES FROM LOCATION MANAGER 
    PendingIntent pi = getLocationPendingIntent(true); 
    mLocationManager.requestLocationUpdates(provider,0,0,pi); 
} 

private void broadcastLocation (Location location) 
{ 
    Intent broadcast = new Intent(ACTION_LOCATION); 
    broadcast.putExtra(LocationManager.KEY_LOCATION_CHANGED, location); 
    mAppContext.sendBroadcast(broadcast); 
} 

public void stopLocationUpdates() 
{ 
    PendingIntent pi = getLocationPendingIntent(false); 

    if (pi != null) 
    { 
     mLocationManager.removeUpdates(pi); 
     pi.cancel(); 
    } 
} 
} 

UserFitness.java

public class UserFitness extends Activity { 

private GPSManager mGPSManager; 
private Location mLastLocation; 
private boolean isGPSReady; 
private float mLastLocationMillis; 

private GpsStatus.Listener mGPSListener = new GpsStatus.Listener() 
{ 
    public void onGpsStatusChanged(int event) { 
     switch (event) { 
      case GpsStatus.GPS_EVENT_SATELLITE_STATUS: 
      if (mLastLocation != null) 

      isGPSReady = (SystemClock.elapsedRealtime() - mLastLocationMillis) < 13000; 

      if (isGPSReady) { // A fix has been acquired. 
       calculateUserDistance() 
      } else { // The fix has been lost. 
       Toast.makeText(getApplicationContext(), "GPS Signal Fix Lost", Toast.LENGTH_SHORT).show(); 
      } 
      break; 
      case GpsStatus.GPS_EVENT_FIRST_FIX: 

      isGPSReady = true; 
      break; 
     } 
    } 
}; 

private BroadcastReceiver mLocationReceiver = new LocationReceiver() 
{ 
    @Override 
    protected void onLocationReceived (Context context, Location loc) 
    { 
     mLastLocationMillis = SystemClock.elapsedRealtime(); 
     mLastLocation = loc; 
     Log.d("OnLocationReceived",mLastLocation.toString()); 
    } 

    protected void onProviderEnabledChanged (boolean enabled) 
    { 

    } 
}; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_fitness); 
    mGPSManager = GPSManager.get(this,mGPSListener); 
} 

@Override 
public void onStart() 
{ 
    super.onStart(); 
    this.registerReceiver(mLocationReceiver, new IntentFilter(GPSManager.ACTION_LOCATION)); 
    mGPSManager.startLocationUpdates(); 
} 

@Override 
public void onStop() 
{ 
    this.unregisterReceiver(mLocationReceiver); 
    super.onStop(); 
} 

/* private void calculateUserDistance() */ 

回答

1

哦,不!這是處理位置更新的舊的棄用方式!首先,你必須熟悉this guideline。 本指南描述了使用LocationClient位置跟蹤策略。

請注意,有跟蹤當前位置的甚至新的方式 - 與LocationServices結合使用GoogleApiClient,但你可以忽略它,由於缺乏適當的文件:(

返回LocationClient - 有2種方式接收位置的更新:

如果你想創建一個健身應用程序 - 你肯定會想看看ActivityRecognition API

+0

非常感謝你的快速反應。我很失望,聽到他們的方式我實施的東西已被棄用。我正在關注開發者書籍「Android Programming:The Big Nerd Ranch Guide」。我將立即查看LocationClient – Teddy13 2014-10-05 02:46:41

+0

是的..更快更好 - LocationClient在電池方面做得好得多,所以它可能對您的應用至關重要 – 2014-10-05 02:54:05

+0

聽起來完全像我所需要的。我會接受你的回答。謝謝! – Teddy13 2014-10-05 03:02:25