GoogleApiClient陷入连接状态,调试报告没有错误

时间:2017-01-02 21:24:04

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

正如标题所示,由于某些原因,谷歌API客户端陷入连接状态,调试信息中没有错误。我等了5分钟才得到答复。

以下是跟踪我当前位置的代码。

public class myLocation implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        ActivityCompat.OnRequestPermissionsResultCallback,
        LocationListener
{


    public static final String TAG = myLocation.class.getSimpleName();

    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
    private final static int MY_PERMISSION_ACCESS_FINE_LOCATION = 1;

    //global variables for information passed on from parent class
    public GoogleApiClient mGoogleApiClient = null;
    private Context mParentBase;
    private Activity mParentActivity;
    private PendingIntent mParentIntent;

    private LocationRequest requestLocation;

    public Location mMyLocation;

    /**
     * Constructor For this class
     */
    public myLocation(Context Base, Activity activitiyBase, PendingIntent intent) {
        mParentBase = Base;
        mParentActivity = activitiyBase;
        mParentIntent = intent;
        initialLocationService();
    }

    /**
     *  Initialize the Location Service and API Client
     */

    public void initialLocationService(){
         mGoogleApiClient = new GoogleApiClient.Builder(mParentBase)
             .addConnectionCallbacks(this)
             .addOnConnectionFailedListener(this)
             .addApi(LocationServices.API)
             .build();

         requestLocation = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(10*1000)
            .setFastestInterval(1*1000);

         Log.d(TAG, "Initialization Location Service");
     }

    public void connect()
    {
        mGoogleApiClient.connect();
        Log.d(TAG, "Connection Requested");
    }

    public void pause() {
        if (mGoogleApiClient.isConnected()) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mParentIntent);
            mGoogleApiClient.disconnect();
        }
    }

    public void disconnect() {
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }

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

        Log.d(TAG, "OnConnected");

        if (PackageManager.PERMISSION_GRANTED ==
                (ContextCompat.checkSelfPermission(mParentBase, android.Manifest.permission.ACCESS_FINE_LOCATION)))

        {
            Log.d(TAG, "Permission Was Granted" );
            mMyLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

            if (mMyLocation == null) {
                LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, requestLocation, mParentIntent);
            } else {
                Log.d(TAG, mMyLocation.toString());
            }
        } else {
            ActivityCompat.requestPermissions(mParentActivity, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_ACCESS_FINE_LOCATION);
            Log.d(TAG, "Permission Was Requested" );
        }

    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
                                           @NonNull int[] grantResults)
    {
        switch (requestCode) {
            case MY_PERMISSION_ACCESS_FINE_LOCATION: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    if (PackageManager.PERMISSION_GRANTED ==
                            (ContextCompat.checkSelfPermission(mParentBase, android.Manifest.permission.ACCESS_FINE_LOCATION)))

                    {
                        mMyLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

                        if (mMyLocation == null) {
                            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, requestLocation, mParentIntent);
                        } else {
                            Log.d(TAG, mMyLocation.toString());
                        }
                    } else {
                        ActivityCompat.requestPermissions(mParentActivity, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                                MY_PERMISSION_ACCESS_FINE_LOCATION);
                    }

                }
            }
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.i(TAG, "Location services suspended. Please Reconnect.");
    }

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

        Log.i(TAG, "Connection Failed");

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

    }

    public void onLocationChanged(Location location) {
        mMyLocation = location;
        Log.d(TAG, location.toString());
    }
}

以下是调用我的位置对象的代码:

public class selectRouteAndTransportMethod extends AppCompatActivity {

    Intent mIntent;
    PendingIntent mPendingIntent;
    myLocation mCurrentLocation;
    public static final String TAG = selectRouteAndTransportMethod.class.getSimpleName();
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_route_and_transport_method);

        mIntent = new Intent(this, selectRouteAndTransportMethod.class);
        mPendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
        mCurrentLocation = new myLocation(this, this, mPendingIntent);
        // ATTENTION: This was auto-generated to implement the App Indexing API.

        // See https://g.co/AppIndexing/AndroidStudio for more information.

    }

    @Override
    protected void onStart() {
        super.onStart();

        Log.d(TAG, "On Start");

        while (mCurrentLocation.mGoogleApiClient == null) {
            Log.d(TAG, "Google ApiClient Not Instantiated");
        }
        mCurrentLocation.connect();

        while (true) {
            boolean connecting = mCurrentLocation.mGoogleApiClient.isConnecting();
            boolean registered = mCurrentLocation.mGoogleApiClient.isConnectionCallbacksRegistered(mCurrentLocation);
            //ConnectionResult connectionResult = mCurrentLocation.mGoogleApiClient.getConnectionResult(LocationServices.API);

            if (connecting == TRUE) {
                Log.d(TAG, "Connecting");
            }

            if (registered == TRUE) {
                Log.d(TAG, "registered");
            }

              //  Log.d(TAG, connectionResult.toString());

            if (mCurrentLocation.mMyLocation != null) {
                break;
            }
        }
    }
}

以下是调试日志的输出。

D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/selectRouteAndTransportMethod: Connecting

1 个答案:

答案 0 :(得分:1)

我认为这与您的while(true) {}循环有关。请尝试将其更改为if-statement。发生了什么事情,你只是把自己置于无限循环的困境中。您可以在调试日志中看到无限循环的证明。