我正在构建一个可以远距离跟踪用户(驾驶时间)的应用程序。它工作但我得到的区域(30分钟加)没有任何跟踪。
我已经尝试添加一个唤醒锁来解决这个问题,但无济于事......所以我认为它没有正确更改提供商?
public class ActiveJobActivity extends Activity实现了GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener,LocationListener,com.google.android.gms.location.LocationListener {
// Milliseconds per second
private static final int MILLISECONDS_PER_SECOND = 1000;
// Update frequency in seconds
public static final int UPDATE_INTERVAL_IN_SECONDS = 10;
// Update frequency in milliseconds
private static final long UPDATE_INTERVAL = MILLISECONDS_PER_SECOND * UPDATE_INTERVAL_IN_SECONDS;
// The fastest update frequency, in seconds
private static final int FASTEST_INTERVAL_IN_SECONDS = 10;
// A fast frequency ceiling in milliseconds
private static final long FASTEST_INTERVAL =
MILLISECONDS_PER_SECOND * FASTEST_INTERVAL_IN_SECONDS;
// vars..
// Define an object that holds accuracy and frequency parameters
LocationRequest mLocationRequest;
LocationClient mLocationClient;
boolean mUpdatesRequested;
protected PowerManager.WakeLock mWakeLock;
LocationManager locationManager;
@Override
protected void onStart() {
super.onStart();
final PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
this.mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Wakelock");
mWakeLock.acquire();
}
/* Prevent app from being killed on back */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Back?
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Back
moveTaskToBack(true);
return true;
}
else {
// Return
return super.onKeyDown(keyCode, event);
}
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_active_job);
if(mLocationRequest == null){
//Create LocationRequest object.
mLocationRequest = LocationRequest.create();
// Use high accuracy
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// Set the update interval to 5 seconds
mLocationRequest.setInterval(UPDATE_INTERVAL);
// Set the fastest update interval to 1 second
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
}
/* Create a new location client, using the enclosing class to
* handle callbacks.
*/
if(mLocationClient == null){
mLocationClient = new LocationClient(this, this, this);
mUpdatesRequested = true;
}
if(!mLocationClient.isConnected()){
mLocationClient.connect();
}
mUpdatesRequested = true;
}
/*
* Handle results returned to the FragmentActivity
* by Google Play services
*/
@Override
protected void onActivityResult(
int requestCode, int resultCode, Intent data) {
// Decide what to do based on the original request code
switch (requestCode) {
case CONNECTION_FAILURE_RESOLUTION_REQUEST :
/*
* If the result code is Activity.RESULT_OK, try
* to connect again
*/
switch (resultCode) {
case Activity.RESULT_OK :
/*
* Try the request again
*/
break;
}
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (connectionResult.hasResolution()) {
try {
// Start an Activity that tries to resolve the error
connectionResult.startResolutionForResult(
this,
CONNECTION_FAILURE_RESOLUTION_REQUEST);
/*
* Thrown if Google Play services cancelled the original
* PendingIntent
*/
} catch (IntentSender.SendIntentException e) {
// Log the error
e.printStackTrace();
}
} else {
}
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
if (mUpdatesRequested) {
mLocationClient.requestLocationUpdates(mLocationRequest, this);
}
}
@Override
public void onDisconnected() {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// add locaiton to db.
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
if(provider.equals("LocationManager.GPS_PROVIDER")){
Log.i("Active Job Location", "GPS Disabled");
}
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
} else {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
mLocationClient.removeLocationUpdates(this);
mLocationClient.requestLocationUpdates(mLocationRequest, this);
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
} else {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
mLocationClient.removeLocationUpdates(this);
mLocationClient.requestLocationUpdates(mLocationRequest, this);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onDestroy(){
super.onDestroy();
if (mWakeLock.isHeld())
{
this.mWakeLock.release();
}
}
}