0

我尝试了谷歌登录代码片段,但不是将它放在一个活动上,而是将它用在一个片段上。它只适用于牛轧糖。这个问题似乎依赖于给予Google的意图。只要我点击Google登录,它就会关闭我的应用。谷歌标志不在低于牛轧糖的API上工作

该对话框显示在我的应用程序外部,它不会回复到我的片段。它只是关闭。

所以这是我的代码:它几乎与代码片段相同。但这里的问题是,当我点击登录Google时应用程序关闭时,它允许用户每次选择电子邮件,但它不会返回到onActivityResult,因为我的logcat上没有记录的数据

public class LoginFragment extends Fragment implements OnConnectionFailedListener { 
    private FragmentManager fragmentManager; 
    private FragmentTransaction fragmentTransaction; 
    private Button signUp, login; 
    private View view; 
    private SharedPref pref; 


    private SignInButton signInButton; 
    private GoogleSignInOptions gso; 
    private GoogleApiClient mGoogleApiClient; 
    private int RC_SIGN_IN = 0; 
    private static final String TAG = "GPlusFragment"; 

    private Retrofit retrofit; 
    private RequestInterface requestInterface; 
    private ServerRequest request; 
    private boolean connectionError = false; 
    @Override 
    public void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
       .requestEmail() 
       .requestProfile() 
       .build(); 

     mGoogleApiClient = new GoogleApiClient.Builder(getActivity()) 
       .enableAutoManage(getActivity() /* FragmentActivity */, this /* OnConnectionFailedListener */) 
       .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
       .build(); 
    } 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     view = inflater.inflate(R.layout.login_layout, container, false); 
     signUp = (Button) view.findViewById(R.id.signup); 
     login = (Button) view.findViewById(R.id.login); 
     fragmentManager = getFragmentManager(); 


     //Initializing signinbutton 
     signInButton = (SignInButton) view.findViewById(R.id.google_sign_in_button); 
     signInButton.setSize(SignInButton.SIZE_WIDE); 

     return view; 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     mGoogleApiClient.stopAutoManage(getActivity()); 
     mGoogleApiClient.disconnect(); 
     connectionError = true; 
     signInButton.setVisibility(View.GONE); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     //mGoogleApiClient.stopAutoManage(getActivity()); 
     mGoogleApiClient.disconnect(); 
    } 

    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     if (connectionError){ 
      mGoogleApiClient.stopAutoManage(getActivity()); 
      mGoogleApiClient.disconnect(); 
      signInButton.setVisibility(View.GONE); 
     } 
     signUp.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       fragmentTransaction = fragmentManager.beginTransaction().addToBackStack("signup").replace(R.id.content_view, new RegisterFragment()); 
       fragmentTransaction.commit(); 
      } 
     }); 
     login.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       fragmentTransaction = fragmentManager.beginTransaction().addToBackStack("signin").replace(R.id.content_view, new SignInFragment()); 
       fragmentTransaction.commit(); 
      } 
     }); 

     signInButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Auth.GoogleSignInApi.signOut(mGoogleApiClient); 
       Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
       startActivityForResult(signInIntent, RC_SIGN_IN); 
      } 
     }); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     Log.d("It's here", "here"); 
     if (requestCode == RC_SIGN_IN) { 
       GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
       handleSignInResult(result); 
      } 
    } 

    private void handleSignInResult(GoogleSignInResult result) { 
     Log.d(TAG, "handleSignInResult:" + result.isSuccess()); 
     if (result.isSuccess()) { 
      //my code here doing the login 
      } 
    } 

    public void goToProfile(){ 
     Intent intent = new Intent(getContext(), HomeLoggedInActivity.class); 
     startActivity(intent); 
    } 
} 

回答

0

你必须使用的,而不是仿真器中的实际的Android手机,因为它有一个老版本的谷歌的发挥

+0

那么,有些人测试了它,并告诉谷歌登录什么都不做。 如果你想检查应用程序本身:https://play.google.com/store/apps/details?id = com.infoman.ibenta –

1

的问题已通过删除 的Android解决:在清单noHistory =“真”。这是防止用户返回登录活动的解决方案。

+0

如果你的问题已经解决,请将此标记为答案 –

+0

我'两天后会做。 –

相关问题