我似乎无法连接到GoogleAPI客户端,没有连接,也没有失败

时间:2016-12-31 17:03:32

标签: java android google-api

所以我正在尝试连接到Google API client,但我没有收到onConnected方法或onConnectionFailed方法的回复。两种方法都是这样的,分别是:

 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 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());
    }

}

我有一个公共接口,可以启动与API的连接:

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

我可以在Android Studio 运行控制台上看到上述日志输出文件。

还有其他人见过这类问题吗?

编辑: 用于构建和初始化客户端的代码

public myLocation(Context Base, Activity activitiyBase, PendingIntent intent) {
        mParentBase = Base;
        mParentActivity = activitiyBase;
        mParentIntent = intent;
        initialLocationService();
    }

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

    private 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);

该类实现以下内容:

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

1 个答案:

答案 0 :(得分:0)

行。我已经汇总了一些代码,希望能帮助您找到问题所在。我将此代码作为设备上的工作应用程序。我已经将你的代码与我的混合使用了。通过比较差异,您应该能够缩小您的问题范围。第一个myLocation类......

onCreate()

在活动中,mMyLocation = new myLocation(getApplicationContext(), this); ...

onStart()

并在mMyLocation.connect(); ...

A=[NaN 2 NaN];
B=[1 NaN 3];

我希望这会有所帮助。