我正在使用位置服务api获取当前位置。实际上,我已经在onresume()方法中使用location manager类验证了location是否已启用,如果未启用,则使用intent将用户发送到location设置。在启用位置返回活动并尝试连接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()方法。请有人告诉我解决方案,因为它是非常重要的。

最佳答案

您可以使用startActivityForResult(),然后在onActivityResult()中检查是否从对话框提示中启用了一个位置提供程序。
如果启用了GPS或网络位置提供程序,则调用mGoogleApiClient.connect()
还要注意,您可以在初始检查中使用&&,而不是||,因为如果至少启用了一个位置提供程序,则实际上不需要提示用户“location is disabled”(位置已禁用)。

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

        }
    }
}

关于android - Google Client Api在 Activity 恢复时未连接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30588105/

10-12 00:23
查看更多