我是Android的新手,从iOS迁移。我正在开发一个健身应用程序(4.3+)来计算用户位置并跟踪它。一切都很好,除非应用程序进入后台,我停止接收位置更新。
我目前正在使用从我的GPSManager类到UserFitness类的BroadcastReceiver。在GPSManager类中,我启动了LocationManager,其他一切包括开始位置更新。然后,每当新的位置更新可用并且在此处完成一些计算,存储并显示给用户时,它就被广播到UserFitness类。我已经研究过我需要为LocationManager实现一个Service才能在后台工作,但我不知道如何为我当前的代码实现它。我的所有代码都在下面。
任何人都可以告诉我如何根据我的代码在后台获取位置更新,这是否可以在没有系统破坏过程的情况下一次大约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() */
答案 0 :(得分:1)
哦,不!这是一种处理位置更新的旧方法!首先,你必须熟悉this guideline。 本指南使用LocationClient描述了位置跟踪策略。
请注意,甚至有更新的方式来跟踪当前位置 - 使用GoogleApiClient和LocationServices,但由于缺少适当的文档,您现在可以忽略它:(< / em>的
返回LocationClient - 有两种方法可以接收位置更新:
如果你想创建一个健身应用程序 - 你肯定想看看ActivityRecognition API