我写了一些代码来获得一个连续跟踪的位置,但是更新仅在屏幕超时后重新启动应用程序时触发,但不是总是...
在" Google Play服务"中,我只使用com.google.android.gms:play-services-location:8.3.0(以减小尺寸)。
AndroidManifest.xml设置为ACCESS_FINE_LOCATION"允许。 GPS和" Wi-Fi&移动网络位置"我的设备已开启。
提前感谢您的帮助。 这是主要部分:
import android.location.Location;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import comm.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
public class MainActivity extends AppCompatActivity implements
ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
protected TextView txtlat, txtlong;
// ---------------------------------------------------------------------------------------
// Create
// ---------------------------------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtlat = (TextView)findViewById(R.id.txtlat);
txtlong = (TextView)findViewById(R.id.txtlong);
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
createLocationRequest();
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000); // 10sec
mLocationRequest.setFastestInterval(5000); // 5sec
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
// ----------------------------------------------------------------------
@Override
protected void onResume() {
super.onResume();
mGoogleApiClient.connect();
}
// ---------------------------------------------------------------------
@Override
protected void onPause() {
super.onPause();
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
}
// ---------------------------------------------------------------------
@Override
public void onConnected(Bundle connectionHint) {
Location location=LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} else {
displayLocation(location);
}
}
// ---------------------------------------------------------------------
public void onLocationChanged (Location location) {
displayLocation(location);
}
// ---------------------------------------------------------------------
@Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
}