谷歌播放服务位置监听器没有被调用,但程序仍然需要它工作?

时间:2017-02-15 20:57:58

标签: java android google-maps google-play-services

正如我在问题中所述,我正在实施位置服务(谷歌不是Android版本),我试着从方法干杯看到它被调用,但我没有运气。如果我删除它虽然我在这一行上出错,因此无法请求位置更新

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

是位置监听器库的一部分还是什么?我无法理解为什么当我改变位置时该应用程序不会举杯祝酒。我已经研究了很多,但找不到答案,所以我在这里问。它正在拾取一个位置并在我的handleNewLocation方法中烘烤纬度。

这是我的代码

package com.example.mick.mylocation;

import android.app.Activity;
import android.content.IntentSender;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.GoogleMap;

public class MainActivity extends Activity implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener
        {
    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
    private LocationRequest mLocationRequest;

    private GoogleApiClient mGoogleApiClient;
    public static final String TAG = MainActivity.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLocationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(10 * 1000)        // 10 seconds, in milliseconds
                .setFastestInterval(1 * 1000); // 1 second, in milliseconds


        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

    }

    @Override
    protected void onResume() {
        super.onResume();
        mGoogleApiClient.connect();
    }

    @Override
    protected void onPause() {
        Toast.makeText(getApplicationContext(),"PAUSED ", Toast.LENGTH_LONG).show();

        super.onPause();
        if (mGoogleApiClient.isConnected()) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
            mGoogleApiClient.disconnect();
        }
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {

            Toast.makeText(getApplicationContext(), "Location services conntected", Toast.LENGTH_LONG).show();
            Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            if (location == null) {
                LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
            } else {
                handleNewLocation(location);
            }

        }


    private void handleNewLocation(Location location) {
        Toast.makeText(getApplicationContext(),"HANNDLE NEW LOCATION METHOD lat is "+location.getLatitude(), Toast.LENGTH_LONG).show();


    }

    @Override
    public void onConnectionSuspended(int i) {
        Toast.makeText(getApplicationContext(),"SUSPENDED", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

        if (connectionResult.hasResolution()) {
            try {
                // Start an Activity that tries to resolve the error
                connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
            } catch (IntentSender.SendIntentException e) {
                e.printStackTrace();
            }
        } else {
            Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
            Toast.makeText(getApplicationContext(),"Location services connection failed with code " + connectionResult.getErrorCode(), Toast.LENGTH_LONG).show();

        }

    }


    @Override
    public void onLocationChanged(Location location) {
        Toast.makeText(getApplicationContext(),"ON LOCATION CHANGED METHOD", Toast.LENGTH_LONG).show();

//        handleNewLocation(location);
    }

}

由于

2 个答案:

答案 0 :(得分:0)

我的猜测是你永远不会在requestLocationUpdates()中进行第一次onConnect()。仅仅因为你新连接并不意味着getLastLocation()将为空。事实上,您希望不会尽快更新显示器。

试试这个:

    if (location != null) {
        handleNewLocation(location);
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

每次连接时发送新的requestLocationUpdates()都不会有什么坏处。 (事实上​​,我认为如果配置更改导致您断开连接并重新连接到GoogleApiClient,则需要发送新请求)

答案 1 :(得分:-2)

如果我是你,我会通过使用ReactiveLocation:

来避免不必要的样板和处理googleApiClients的麻烦
public class MainActivity extends Activity {

     public static final String TAG = MainActivity.class.getSimpleName();
     private Subscription subscription;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(10 * 1000)        // 10 seconds, in milliseconds
            .setFastestInterval(1 * 1000); // 1 second, in milliseconds


         ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(context);
         subscription = locationProvider.getUpdatedLocation(mLocationRequest)
            .subscribe(new Action1<Location>() {
                @Override
                public void call(Location location) {
                    //This will be called every time a new location is received
                    Toast.makeText(getApplicationContext(), "Location updated", Toast.LENGTH_LONG).show();
                }
            });

    }

    @Override
    public void onPause() {
         //Make sure to stop listening for updates when you don't need them anymore
         subscription.unsubscribe();
    }
}