67206
Google Map Apiを使ったアプリを作成しようとしています。

そこで下記コードでGoogleApiClientの初期化を試みましたがonConnectedが呼ばれません。

コード

```

    public class MainActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener,
            OnMapReadyCallback,
            GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener,
            LocationListener {
    
        private SampleApplication mApplication;
        private GoogleApiClient mGoogleApiClient;
        private GoogleMap mGoogleMap;
        private LocationRequest mLocationRequest;
        @State Location mCurrentLocation;
        @State boolean mRequestingLocationUpdate;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            /**
             * サインイン済みかどうかをチェックする
             * サインインしていない場合、SigninActivityを立ち上げサインインさせる
             */
            /*
            if (!readUserInfo()) {
                Intent intent = new Intent(MainActivity.this, SigninActivity.class);
                startActivity(intent);
                finish();
            }
            */
    
            /**
             * ログインチェック終わり
             */
    
    
            setContentView(R.layout.activity_main);
            // Use ButterKnife
            ButterKnife.bind(this);
            // Use icepick
            Icepick.restoreInstanceState(this, savedInstanceState);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });
    
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.setDrawerListener(toggle);
            toggle.syncState();
    
            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);
    
            mApplication = (SampleApplication) getApplication();
            MapFragment mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
    
            buildGoogleApiClient();
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            if(mGoogleApiClient != null&&mGoogleApiClient.isConnected()&&!mRequestingLocationUpdate){
                startLocationUpdate();
            }
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            stopLocationUpdate();
        }
    
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            Icepick.saveInstanceState(this, outState);
        }
    
        @Override
        public void onBackPressed() {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                super.onBackPressed();
            }
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
        @SuppressWarnings("StatementWithEmptyBody")
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            // Handle navigation view item clicks here.
            int id = item.getItemId();
    
            if (id == R.id.nav_camara) {
                // Handle the camera action
            } else if (id == R.id.nav_gallery) {
    
            } else if (id == R.id.nav_slideshow) {
    
            } else if (id == R.id.nav_manage) {
    
            } else if (id == R.id.nav_share) {
    
            } else if (id == R.id.nav_send) {
    
            }
    
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
    
        @Override
        public void onMapReady(GoogleMap googleMap) {
            Log.d("==debug==", "Initialized Google map");
            mGoogleMap = googleMap;
            mGoogleMap.setMyLocationEnabled(true);
            //googleMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Title"));
            createLocationRequest();
        }
    
        @Override
        public void onConnected(Bundle bundle) {
            Log.d("==debug==", "Connected to Google API");
            if(mRequestingLocationUpdate){
                startLocationUpdate();
            }
        }
    
        @Override
        public void onConnectionSuspended(int i) {
            Log.d("==debug==", "Connected suspended");
        }
    
        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {
            Log.d("==debug==", "Connection failed" + connectionResult.getErrorMessage());
        }
    
        @Override
        public void onLocationChanged(Location location) {
            mCurrentLocation = location;
            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(
                    new LatLng(
                            mCurrentLocation.getLatitude(),
                            mCurrentLocation.getLongitude()), 15);
            mGoogleMap.moveCamera(cameraUpdate);
        }
    
        private boolean readUserInfo(){
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
            String consumerKey = prefs.getString("consumerKey", null);
            if(consumerKey == null)
                return false;
            Long userId = prefs.getLong("userId", 0);
            if(userId == 0)
                return false;
            String accessToken = prefs.getString("accessToken", null);
            if(accessToken == null)
                return false;
    
            //ConsumerKeyとUserIdを設定ファイルに保存
            mApplication.consumerKey=consumerKey;
            mApplication.userId=userId;
            mApplication.accessToken=accessToken;
            return true;
        }
    
        private synchronized void buildGoogleApiClient(){
            Log.d("==debug==", "Building Google API client");
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
        }
    
        private void createLocationRequest(){
            Log.d("==debug==", "Creating location request");
            mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(1000);
            mLocationRequest.setInterval(800);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        }
    
        private void startLocationUpdate(){
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        }
    
        private void stopLocationUpdate(){
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }
    
    
    }
    
```

このコードの中に複数デバッグのログを出すようにしていますが、onConnected,onConnectionSuspended,onConnectionFailedが呼ばれません。他のデバッグ文は呼ばれています。  
logcatにもエラーのようなものは見られませんでした。  
どこが間違っているかわかりません。どなたかアドバイスをいただけないでしょうか。よろしくお願いします。
CC BY-SA 3.0
2015-11-01T02:27:10.620
18289
2020-06-02T01:03:46.867
7232
question
android java google-maps
Google Api ClientのonConnectedが呼ばれない
`buildGoogleApiClient()` メソッドで `mGoogleApiClient.connect();`
を実行する必要があるのではないでしょうか。

* * *

_この投稿は[@user9156 さんのコメント](https://ja.stackoverflow.com/questions/18289/google-
api-
client%e3%81%aeonconnected%e3%81%8c%e5%91%bc%e3%81%b0%e3%82%8c%e3%81%aa%e3%81%84#comment16927_18289)
の内容を元に コミュニティwiki として投稿しました。_
CC BY-SA 4.0
2020-06-02T01:03:46.867
67206
2020-06-02T01:03:46.867
3060
18289
answer