2017-07-20 11 views
-1

私は自分で登録し、登録された詳細でログインできる1つのアプリケーションを作成しています。私がしなければならないのは、Googleのログインボタンを追加したいということです。ユーザーがこのボタンをクリックすると、Googleアカウントでログインできます。私はこれについて多くを検索しましたが、私は活動の中でGoogleのコードをサインインしました。いくつかのどのようなこのユーザーはGoogleアカウントが、次の活動が開きますと、ユーザー情報が次の活動の中で理解すべきでサインインし私たちのアプリにGoogleログインを追加する方法

public class MainActivity extends AppCompatActivity implements View.OnClickListener,GoogleApiClient.OnConnectionFailedListener { 

private LinearLayout Prof_Section; 
private Button SignOut; 
private SignInButton SignIn; 
private TextView Name, Email; 
private ImageView Prof_Pic; 
private GoogleApiClient googleApiClient; 
private static final int REQ_CODE = 9001; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Prof_Section = (LinearLayout) findViewById(R.id.prof_section); 
    SignOut = (Button) findViewById(R.id.bn_logout); 
    SignIn = (SignInButton) findViewById(R.id.bn_login); 
    Name = (TextView) findViewById(R.id.name); 
    Email = (TextView) findViewById(R.id.email); 
    Prof_Pic = (ImageView) findViewById(R.id.prof_pic); 
    SignIn.setOnClickListener(this); 
    SignOut.setOnClickListener(this); 
    Prof_Section.setVisibility(View.GONE); 
    GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build(); 
    googleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this,this).addApi(Auth.GOOGLE_SIGN_IN_API, signInOptions).build(); 

} 

@Override 
public void onClick(View v) { 

    switch (v.getId()) { 
     case R.id.bn_login: 
      SignIn(); 
      break; 
     case R.id.bn_logout: 
      SignOut(); 
      break; 
    } 
} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 

private void SignIn() { 
    Intent Intent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient); 
    startActivityForResult(Intent, REQ_CODE); 

} 

private void SignOut() { 
    Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(new ResultCallback<Status>() { 
     @Override 
     public void onResult(@NonNull Status status) { 
      updateUI(false); 
     } 
    }); 
} 

private void handleResult(GoogleSignInResult result) { 
    if (result.isSuccess()) { 
     GoogleSignInAccount account = result.getSignInAccount(); 

     String name = account.getDisplayName(); 
     String email = account.getEmail(); 
     String img_url = account.getPhotoUrl().toString(); 
     Name.setText(name); 
     Email.setText(email); 
     Glide.with(this).load(img_url).into(Prof_Pic); 
     updateUI(true); 
    } else { 
     updateUI(false); 

    } 
} 

private void updateUI(boolean isLogin) { 

    if (isLogin) { 
     Prof_Section.setVisibility(View.VISIBLE); 
     SignIn.setVisibility(View.GONE); 
    } else { 
     Prof_Section.setVisibility(View.GONE); 
     SignIn.setVisibility(View.VISIBLE); 
    } 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == REQ_CODE) { 

     GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
     handleResult(result); 
    } 
} 

}である私は何をしたい

、。そして私は彼らのユーザーネーム、電子メールと連絡先の番号がmysqlデータベースに格納される必要があります。何をすべきか教えてください。それが可能かどうか?

+0

このリンクに記載されている手順に従ってください。https://developers.google.com/identity/sign-in/android/start-integrating –

+0

Bro、既にリンクを終えました。私は既にGoogleログインを追加していますユーザーがサインインボタンをクリックすると新しいアクティビティが開き、Googleのプロフィール情報がそこに表示されるはずです。 –

+0

google-services.jsonファイルを生成し、有効なGoogleログインをしましたか?このリンクを参照してくださいhttps://developers.google.com/mobile/add?platform=android&cntapi=signin&cnturl=https:%2F%2Fdevelopers.google.com%2Fidentity%2Fsign-in%2Fandroid%2Fsign-in%3Fconfigured%3Dtrue&cntlbl= %20Adding%20Adding%20Sign-In –

答えて

0

このようにコードを変更します。

private void handleResult(GoogleSignInResult result) { 
     if (result.isSuccess()) { 
      GoogleSignInAccount account = result.getSignInAccount(); 

      String name = account.getDisplayName(); 
      String email = account.getEmail(); 
      String img_url = account.getPhotoUrl().toString(); 
      Name.setText(name); 
      Email.setText(email); 
      Glide.with(this).load(img_url).into(Prof_Pic); 
      //updateUI(true); 
      Intent intent =new Intent(MainActivity.this,YourNewActivity.class); 
      intent.putExtra("name",name); 
      intent.putExtra("email",email); 
      intent.putExtra("img_url",img_url); 
      startActivity(intent); 
      } else { 
      //updateUI(false); 
      } 

getintentを使用して、呼び出されたアクティビティでMainActivityから送信した詳細を取得し、そこに詳細を表示します。また、SQLデータベースに簡単に送信してそこに保存することもできます。

関連する問題