onLocationChanged从未调用,GoogleApiClient

时间:2017-05-17 10:50:32

标签: android google-maps geolocation google-api-client

我正在尝试通过运行后台服务来获得连续的位置更新。当我调试代码时,调用 onConnected(Bundle b)并调用位置更新请求。但是永远不会调用 onLocationChanged(位置位置)。以下是我的代码:

$data=['a','b','c','e','r','t','x','s','b','a','b','c'];    // No Match
$data=['n','o','p','e'];                                    // No Match
$data=['r','a','c','e','c','a','r'];                        // ['r','a','c','e','c','a','r']
$data=['a','a','b','a','a'];                                // ['a','a','b','a','a']

我无法理解我犯错的地方,而我跟着android docs。我在真实设备上测试,应用程序具有位置权限。

服务于清单:

    public class LocationUpdateService extends Service implements
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {


    private GoogleApiClient googleApiClient;
    private LocationRequest mLocationRequest;
    Location mCurrentLocation;

    @Override
    public void onLocationChanged(Location location) {
        mCurrentLocation = location;
        double lat = mCurrentLocation.getLatitude();
        double lng = mCurrentLocation.getLongitude();
    }

    //GoogleApiClient
    @Override
    public void onConnectionFailed(ConnectionResult bundle) {

    }

    @Override
    public void onConnected(Bundle bundle) {
        Log.i("onConnected", "GoogleApiClient");

        Toast.makeText(this, "Location  service connected", Toast.LENGTH_SHORT).show();
        createLocationRequest();
        startLocationUpdate();
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    //Service
    @Override
    public void onCreate() {
        super.onCreate();
        buildGoogleApiClient();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        return START_STICKY; // run until explicitly stopped.
    }

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

    private void startLocationUpdate() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        LocationServices.FusedLocationApi.requestLocationUpdates(
                googleApiClient, mLocationRequest, this);
        mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    }

    void buildGoogleApiClient() {
        googleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        googleApiClient.connect();
    }

    void createLocationRequest() {
        mLocationRequest = new LocationRequest().create()
                .setInterval(5000)
                .setFastestInterval(5000)
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }
}

活动中的服务呼叫:

<service
            android:name=".locations.LocationUpdateService"
            android:enabled="true"
            android:exported="false"></service>

2 个答案:

答案 0 :(得分:0)

你的android手机SDK是什么?你如何要求清单文件的权限? 如果您的手机SDK为23或更高,则您要求权限的方式也不同。

答案 1 :(得分:0)

这只是一个愚蠢的调试问题。我发现我的设备位置Access已提前关闭。