<uses-permission android:name="android.permission.INTERNET"/>
2. Now add google sign in library
implementation 'com.google.android.gms:play-services-auth:19.0.0'
3. In MainActivity on button click event we open google sign in dialogue from when we get user information’s like name, email, image Uri, user id at below show full code of sign in flow for google sign in.
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.google.android.gms.common.SignInButton android:id="@+id/google_sign_in" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private GoogleSignInClient signInClient; private final int SIGN_IN_REQ_CODE = 123; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // if user already sign in using google account return not null otherwise return null if (getLastSignInAccount(this) != null){ Intent intent = new Intent(this, UserActivity.class); startActivity(intent); // start user activity if user already login with gmail finish(); } SignInButton googleSignIn = findViewById(R.id.google_sign_in); googleSignIn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build(); signInClient = GoogleSignIn.getClient(MainActivity.this, signInOptions); Intent signInIntent = signInClient.getSignInIntent(); startActivityForResult(signInIntent,SIGN_IN_REQ_CODE); } }); } // return null if user is not sign in previously or return last account private GoogleSignInAccount getLastSignInAccount(Context context) { return GoogleSignIn.getLastSignedInAccount(context); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == SIGN_IN_REQ_CODE){ if (data != null){ Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data); try { GoogleSignInAccount account = task.getResult(ApiException.class); String name = account.getDisplayName(); String email = account.getEmail(); // image can be null if user not set in their account String imageUri = ""; if (account.getPhotoUrl() != null){ imageUri = account.getPhotoUrl().toString(); } String userID = account.getId(); Log.d(TAG, "onActivityResult: user name"+name); Log.d(TAG, "onActivityResult: user email"+email); Log.d(TAG, "onActivityResult: user image uri"+imageUri); Log.d(TAG, "onActivityResult: user id"+userID); Intent intent = new Intent(MainActivity.this,UserActivity.class); startActivity(intent); finish(); } catch (ApiException e) { Log.d("TAG", "handleSignInResult: "+e.getMessage()); } } } } }
public class UserActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_user); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu,menu); return true; } @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { if (item.getItemId() == R.id.sign_out){ Intent logout = new Intent(UserActivity.this, MainActivity.class); GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build(); GoogleSignInClient signInClient = GoogleSignIn.getClient(UserActivity.this, signInOptions); signInClient.signOut() .addOnCompleteListener(UserActivity.this, new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { } }); startActivity(logout); finish(); } return super.onOptionsItemSelected(item); } }
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular