Google客户端Api未在活动恢复

时间:2015-06-02 05:34:36

标签: android android-googleapiclient

我使用位置服务API获取当前位置。实际上,我已经在onResume()方法中使用位置管理器类验证了位置是否已启用,如果未启用,则使用intent将用户发送到位置设置。启用位置返回活动并尝试连接Google客户端API后。但它没有连接,也没有提供任何位置。我使用过这段代码。

protected synchronized void buildGoogleApiClient(){
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(connectionCallbackListener)
            .addOnConnectionFailedListener(connectionFailedListener)
            .addApi(LocationServices.API)
            .build();
}

@Override
protected void onResume() {
    super.onResume();
    if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
            !manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("Location is disabled")
                .setMessage("Please enable your location")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                    }
                });

        AlertDialog dialog = builder.create();
        dialog.show();

    } else {
        Log.v("Connection Status", String.valueOf(mGoogleApiClient.isConnected()));
        mGoogleApiClient.connect();
    }
}

@Override
protected void onPause() {
    super.onPause();
    if (mGoogleApiClient.isConnected()){
        mGoogleApiClient.disconnect();
    }
}

GoogleApiClient.ConnectionCallbacks connectionCallbackListener = new GoogleApiClient.ConnectionCallbacks() {

    @Override
    public void onConnected(Bundle bundle) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

        if (mLastLocation != null){
            Log.v("Latitude", String.valueOf(mLastLocation.getLatitude()));
            Log.v("LOngitude", String.valueOf(mLastLocation.getLongitude()));
            startIntentService();
        }
    }
    @Override
    public void onConnectionSuspended(int i) {

    }
};

GoogleApiClient.OnConnectionFailedListener connectionFailedListener = new GoogleApiClient.OnConnectionFailedListener() {
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.i("Connection failed", String.valueOf(connectionResult.getErrorCode()));
    }
};

我在onCreate()回调中调用了buildGoogleApiClient()方法。请有人告诉我解决方案,因为它非常重要。

1 个答案:

答案 0 :(得分:3)

您可以使用startActivityForResult(),然后在onActivityResult()中查看是否从对话框提示中启用了其中一个位置提供程序。

如果启用了GPS或网络位置提供商,请拨打mGoogleApiClient.connect()

另请注意,您可以在初始检查中使用&&而不是||,因为如果启用了至少一个位置提供商,则确实无需提示用户“已禁用位置”

@Override
protected void onResume() {
    super.onResume();
    if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER) &&
            !manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("Location is disabled")
                .setMessage("Please enable your location")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), 100);
                    }
                });

        AlertDialog dialog = builder.create();
        dialog.show();

    } else {
        Log.v("Connection Status", String.valueOf(mGoogleApiClient.isConnected()));
        mGoogleApiClient.connect();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 100) {
        if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
                manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {

            //At least one provider enabled, connect GoogleApiClient
            mGoogleApiClient.connect();

        }
    }
}