Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 谷歌客户端Api未在活动恢复中连接_Android_Android Googleapiclient - Fatal编程技术网

Android 谷歌客户端Api未在活动恢复中连接

Android 谷歌客户端Api未在活动恢复中连接,android,android-googleapiclient,Android,Android Googleapiclient,我正在使用位置服务api获取当前位置。实际上,我已经使用onResume()方法中的location Manager类验证了location是否已启用,如果未启用,则使用intent将用户发送到location设置。启用位置后,返回活动并尝试连接Google客户端api。但它没有连接,也没有给出任何位置。我用过这个代码 protected synchronized void buildGoogleApiClient(){ mGoogleApiClient = new GoogleApiC

我正在使用位置服务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()回调中调用buildGoogleAppClient()方法。请有人告诉我解决方案,因为它非常重要。

您可以使用
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();

        }
    }
}

你的日志告诉你什么?@Stephan Branchyk情况是,如果我的位置已经启用,那么我将在日志猫中获取当前位置。但如果尚未启用位置,则我将移动到位置设置并启用它。然后回到活动。但是客户端api没有连接,也没有在log cat中获取当前位置。@MohdAquib我刚刚在我的设备上测试了这个,当我从“设置”中单击“上一步”按钮时,意识到它正在返回
RESULT\u cancelled
。因此,我修改了答案,您只需进行
resultCode
检查,因为在这种情况下并不需要它。我在日志cat中也得到了结果代码0。现在我修改了我的代码..谢谢again@MohdAquib没问题!