Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android signin with Google plus issue on the first login

I implemented signin with google with no problems. I am using a fragment in place of an activity. Testing the code on samsung galaxies it happens that signin system goes idle and do not call onConnected method. This happens just the first time, when the terms and conditions shows. Any advice? Here is my code:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
   super.onActivityCreated(savedInstanceState);
   initGoogle()
}


private void initGoogle() {
    mPlusClient = new PlusClient.Builder(getActivity() , this , this).setVisibleActivities("http://schemas.google.com/AddActivity", "http://schemas.google.com/BuyActivity").build();

}

public void connectWithPlus() {
    launchProgressScreen();
    if(mPlusClient != null && !mPlusClient.isConnected()){
         mPlusClient.connect();
    }else{ 
        mPlusClient.clearDefaultAccount();
        mPlusClient.disconnect();
        mPlusClient.connect();
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_RESOLVE_ERR) {
    mConnectionResult = null;
    connectWithPlus();  
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
    if (result!= null && result.hasResolution()) {
        try {
            result.startResolutionForResult(getActivity(), REQUEST_CODE_RESOLVE_ERR);
        } catch (SendIntentException e) {
            connectWithPlus();
        }
        mConnectionResult = result;
    }else{
        onTaskResult(ProgressAlert.TASK_CANCEL_GOOGLE_LOGIN, null);
    }
}
@Override
public void onConnected(Bundle connectionHint) {
    AsyncTask<Object, Void, String> task = new AsyncTask<Object, Void, String>()    {
        @Override
        protected String doInBackground(Object... params) {
            String token = null;
            try {
                token = GoogleAuthUtil.getToken(getActivity(), mPlusClient.getAccountName(),    
                        "oauth2:" + Scopes.PLUS_LOGIN );
            } catch (UserRecoverableAuthException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (GoogleAuthException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return token;
        }

        @Override
        protected void onPostExecute(String token){
            getAuthDelegate().setToken(token, SocialAccount.google);
            getAuthDelegate().loginUsingGoogle(token, new HashMap<String, String>());
        }
    };
    task.execute();
}

@Override
public void onDisconnected() {
    Log.w("Google Login", "called disconnected");
}

@Override
public void onClick(View v) {
    connectWithPlus();
}
like image 473
Elio Scordo Avatar asked Nov 12 '22 01:11

Elio Scordo


1 Answers

I faced same problem and I fixed it by overriding onResume method in Fragment.

@Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        mGoogleApiClient.connect();
    }

I am still not sure if its valid solution or not.

Thanks, Rahul

like image 167
Rahul Avatar answered Nov 15 '22 11:11

Rahul