如何在我的android应用程序中撤销谷歌帐户访问权限?
我是android新手,想知道如何撤销使用谷歌登录我的应用程序的人的访问权 我希望我的应用程序在用户撤销应用程序内的访问权限时请求用户的google帐户id。目前,我的应用程序会在用户单击“登录”按钮后自动登录,即使在用户单击“撤消访问”按钮后也是如此 当用户单击我的应用程序的撤销或注销按钮时,我正在广播一个意图。下面是我的main活动的代码如何在我的android应用程序中撤销谷歌帐户访问权限?,android,google-signin,Android,Google Signin,我是android新手,想知道如何撤销使用谷歌登录我的应用程序的人的访问权 我希望我的应用程序在用户撤销应用程序内的访问权限时请求用户的google帐户id。目前,我的应用程序会在用户单击“登录”按钮后自动登录,即使在用户单击“撤消访问”按钮后也是如此 当用户单击我的应用程序的撤销或注销按钮时,我正在广播一个意图。下面是我的main活动的代码 public class MasterActivity extends AppCompatActivity implements Navigati
public class MasterActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = MasterActivity.class.getSimpleName();
private SharedPreferences sp;
private SharedPreferences.Editor editor;
private BroadcastReceiver mSignOutReceiver;
private IntentFilter mSignOutFilter;
private IntentFilter mRevokeAccessFilter;
private BroadcastReceiver mRevokeAccessReceiver;
private String mUserName;
private String mPhotoUrl;
private String mEmailId;
private static final String ANONYMOUS = "anonymous";
private ImageView profileImageView;
private TextView profileDisplayName, profileEmailId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// [START Check for sign out broadcast.]
mSignOutFilter = new IntentFilter();
mSignOutFilter.addAction(getString(R.string.action_signout));
mSignOutReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Sign Out in progress.");
Intent signinIntent = new Intent(getApplicationContext(), SigninActivity.class);
startActivity(signinIntent);
finish();
}
};
this.registerReceiver(mSignOutReceiver, mSignOutFilter);
// [END Check for sign out broadcast.]
mRevokeAccessFilter = new IntentFilter();
mRevokeAccessFilter.addAction(getString(R.string.action_revoke));
mRevokeAccessReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Revoke access in progress.");
Intent revokeIntent = new Intent(getApplicationContext(), SigninActivity.class);
startActivity(revokeIntent);
finish();
}
};
this.registerReceiver(mRevokeAccessReceiver, mRevokeAccessFilter);
setContentView(R.layout.activity_master);
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);
sp = getSharedPreferences(getString(R.string.user_cred_sp),MODE_PRIVATE);
editor = sp.edit();
if(!sp.contains("USER_ID")){
//Not signed in, launch the Sign In activity
Log.d(TAG, "User id not is present.");
startActivity(new Intent(this, SigninActivity.class));
finish();
return;
} else {
// [START Set the navigation header details]
mUserName = sp.getString("USER_DISPLAY_NAME",ANONYMOUS);
mPhotoUrl = sp.getString("USER_PIC_URL",null);
mEmailId = sp.getString("USER_EMAIL","noemailid@unknown.com");
View headerView = navigationView.inflateHeaderView(R.layout.nav_header_master);
profileDisplayName = (TextView) headerView.findViewById(R.id.UserNameProfile);
profileDisplayName.setText(mUserName);
profileEmailId = (TextView) headerView.findViewById(R.id.EmailIdProfile);
profileEmailId.setText(mEmailId);
profileImageView = (ImageView) headerView.findViewById(R.id.ImageViewProfile);
if(mPhotoUrl!=null){
Glide.with(getApplicationContext()).load(mPhotoUrl)
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(profileImageView);
}
//TODO: The orientation of views and image is not proper
// [END Set the navigation header details]
}
}
/*@Override
protected void onResume(){
super.onResume();
this.registerReceiver(mSignOutReceiver, signOutFilter);
}*/
@Override
protected void onDestroy(){
super.onDestroy();
this.unregisterReceiver(mSignOutReceiver);
this.unregisterReceiver(mRevokeAccessReceiver);
}
@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.master, 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();
if (id == R.id.action_settings) {
return true;
} else if (id == R.id.sign_out_menu ){
signOutBroadCast();
return true;
} else if (id == R.id.revoke_menu){
revokeAccessBroadCast();
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_logout) {
// 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;
}
private void signOutBroadCast(){
// 1. Clear the shared preference.
editor.clear();
editor.apply();
// 2.Send a sign out broadcast
Intent signOutIntent = new Intent();
signOutIntent.setAction(getString(R.string.action_signout));
sendBroadcast(signOutIntent);
// 3. Start the login Activity
/*Intent signinIntent = new Intent(this,SigninActivity.class);
startActivity(signinIntent);
finish();*/
}
private void revokeAccessBroadCast(){
// 1. Clear the shared preference.
editor.clear();
editor.apply();
// 2.Send a revoke intent.
Intent revokeIntent = new Intent();
revokeIntent.setAction(getString(R.string.action_revoke));
sendBroadcast(revokeIntent);
//signOutBroadCast();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.d(TAG, "onConnectionFailed: "+ connectionResult);
}
}
public class SigninActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener, View.OnClickListener,
GoogleApiClient.ConnectionCallbacks{
private static final String TAG = SigninActivity.class.getSimpleName();
private static final int REQ_ACCPICK = 1;
private static final int RC_SIGN_IN = 2;
private GoogleSignInOptions mGoogleSignInOptions;
private GoogleApiClient mGoogleApiClient;
private SharedPreferences sp;
private SharedPreferences.Editor editor;
private IntentFilter mSignOutFilter;
private BroadcastReceiver mSignOutReceiver;
private IntentFilter mRevokeAccessFilter;
private BroadcastReceiver mRevokeAccessReceiver;
private boolean isAccntConnected;
private SignInButton btnSignIn;
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSignOutFilter = new IntentFilter();
mSignOutFilter.addAction(getString(R.string.action_signout));
mSignOutReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
signOutIfConnected();
Log.d(TAG, "Sign out complete.");
}
};
this.registerReceiver(mSignOutReceiver, mSignOutFilter);
mRevokeAccessFilter = new IntentFilter();
mRevokeAccessFilter.addAction(getString(R.string.action_revoke));
mRevokeAccessReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG,"Revoke access");
revokeAccess();
Log.d(TAG, "Complete access revoked.");
}
};
this.registerReceiver(mRevokeAccessReceiver, mRevokeAccessFilter);
// [START Sign out if connected.]
//signOutIfConnected();
// [END Sign out if connected.]
setContentView(R.layout.activity_signin);
btnSignIn = (SignInButton) findViewById(R.id.btn_sign_in);
btnSignIn.setOnClickListener(this);
// [START Configure sign in]
// configure sign in options for google account
mGoogleSignInOptions= new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
// [END Configure sign in]
isAccntConnected= false;
// [START Build Google api client]
/*mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();*/
// [END Build Google api client]
btnSignIn.setSize(SignInButton.SIZE_STANDARD);
btnSignIn.setScopes(mGoogleSignInOptions.getScopeArray());
sp = getSharedPreferences(getString(R.string.user_cred_sp), MODE_PRIVATE);
editor = sp.edit();
}
/*@Override
protected void onResume(){
super.onResume();
this.registerReceiver(mSignOutReceiver, mSignOutFilter);
this.registerReceiver(mRevokeAccessReceiver, mRevokeAccessFilter);
}*/
/*@Override
protected void onPause(){
super.onPause();
this.unregisterReceiver(mSignOutReceiver);
this.unregisterReceiver(mRevokeAccessReceiver);
}*/
@Override
protected void onDestroy(){
super.onDestroy();
this.unregisterReceiver(mSignOutReceiver);
this.unregisterReceiver(mRevokeAccessReceiver);
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.d(TAG, "onConnectionFailed: " + connectionResult);
}
private void initGAC(){
/*mGoogleSignInOptions= new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.setAccountName(acntName)
.build();*/
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, mGoogleSignInOptions)
//.setAccountName(acntName)
.build();
}
@Override
public void onStart() {
super.onStart();
if(mGoogleApiClient!=null){
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (opr.isDone()) {
// If the user's cached credentials are valid, the OptionalPendingResult will be "done"
// and the GoogleSignInResult will be available instantly.
Log.d(TAG, "Got cached sign in");
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else {
// If the user has not previously signed in on this device or the sign-in has expired,
// this asynchronous branch will attempt to sign in the user silently. Cross-device
// single sign-on will occur in this branch.
showProgressDialog();
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(GoogleSignInResult googleSignInResult) {
hideProgressDialog();
handleSignInResult(googleSignInResult);
}
});
}
}
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.btn_sign_in) {
signIn();
}
}
private void signIn() {
/*startActivityForResult(AccountPicker.newChooseAccountIntent(
null, null, new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE}, true, null, null, null, null
),REQ_ACCPICK);*/
Log.d(TAG, "Sign in method called.");
initGAC();
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);
/*if(requestCode == REQ_ACCPICK){
if(data!=null && data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME)!=null){
mEmail = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
initGAC(mEmail);
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
} else*/
if (requestCode == RC_SIGN_IN) {
Log.d(TAG, "Sign in request");
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Sign in successful. Show authenticated UI
// Set the data in intent and send it to next activity
GoogleSignInAccount acct = result.getSignInAccount();
Log.d(TAG, "Account Profile URL: "+acct.getPhotoUrl().toString());
String userId = acct.getId();
String userDisplayName = acct.getDisplayName();
String userPhotoUrl = acct.getPhotoUrl().toString();
String userEmail = acct.getEmail();
//Set the id in shared preferences so that it can be used to log out
editor.putString("USER_ID", userId);
editor.putString("USER_DISPLAY_NAME", userDisplayName);
editor.putString("USER_PIC_URL", userPhotoUrl);
editor.putString("USER_EMAIL", userEmail);
editor.commit();
//dataIntent.putExtra("USER_EMAIL",userEmail);
Intent dataIntent = new Intent(this, MasterActivity.class);
startActivity(dataIntent);
}
}
private void showProgressDialog() {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage(getString(R.string.loading));
mProgressDialog.setIndeterminate(true);
}
mProgressDialog.show();
}
private void hideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.hide();
}
}
private void signOutIfConnected() {
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
//mEmail=null;
mGoogleApiClient.clearDefaultAccountAndReconnect();
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
Log.d(TAG, "Sign Out using Google Api.");
mGoogleApiClient.disconnect();
isAccntConnected = false;
}
});
}
}
private void revokeAccess(){
if (mGoogleApiClient != null && mGoogleApiClient.isConnected() && isAccntConnected == true) {
Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
Log.d(TAG, "Revoke access using Google Api.");
signOutIfConnected();
}
});
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
this.isAccntConnected = true;
}
@Override
public void onConnectionSuspended(int i) {
}
/*@Override
public void onConnected(@Nullable Bundle bundle) {
if(!sp.contains("USER_ID_TOKEN")){
signOut();
}
}
@Override
public void onConnectionSuspended(int i) {
}*/
}
这是我的重要活动代码
public class MasterActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = MasterActivity.class.getSimpleName();
private SharedPreferences sp;
private SharedPreferences.Editor editor;
private BroadcastReceiver mSignOutReceiver;
private IntentFilter mSignOutFilter;
private IntentFilter mRevokeAccessFilter;
private BroadcastReceiver mRevokeAccessReceiver;
private String mUserName;
private String mPhotoUrl;
private String mEmailId;
private static final String ANONYMOUS = "anonymous";
private ImageView profileImageView;
private TextView profileDisplayName, profileEmailId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// [START Check for sign out broadcast.]
mSignOutFilter = new IntentFilter();
mSignOutFilter.addAction(getString(R.string.action_signout));
mSignOutReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Sign Out in progress.");
Intent signinIntent = new Intent(getApplicationContext(), SigninActivity.class);
startActivity(signinIntent);
finish();
}
};
this.registerReceiver(mSignOutReceiver, mSignOutFilter);
// [END Check for sign out broadcast.]
mRevokeAccessFilter = new IntentFilter();
mRevokeAccessFilter.addAction(getString(R.string.action_revoke));
mRevokeAccessReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Revoke access in progress.");
Intent revokeIntent = new Intent(getApplicationContext(), SigninActivity.class);
startActivity(revokeIntent);
finish();
}
};
this.registerReceiver(mRevokeAccessReceiver, mRevokeAccessFilter);
setContentView(R.layout.activity_master);
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);
sp = getSharedPreferences(getString(R.string.user_cred_sp),MODE_PRIVATE);
editor = sp.edit();
if(!sp.contains("USER_ID")){
//Not signed in, launch the Sign In activity
Log.d(TAG, "User id not is present.");
startActivity(new Intent(this, SigninActivity.class));
finish();
return;
} else {
// [START Set the navigation header details]
mUserName = sp.getString("USER_DISPLAY_NAME",ANONYMOUS);
mPhotoUrl = sp.getString("USER_PIC_URL",null);
mEmailId = sp.getString("USER_EMAIL","noemailid@unknown.com");
View headerView = navigationView.inflateHeaderView(R.layout.nav_header_master);
profileDisplayName = (TextView) headerView.findViewById(R.id.UserNameProfile);
profileDisplayName.setText(mUserName);
profileEmailId = (TextView) headerView.findViewById(R.id.EmailIdProfile);
profileEmailId.setText(mEmailId);
profileImageView = (ImageView) headerView.findViewById(R.id.ImageViewProfile);
if(mPhotoUrl!=null){
Glide.with(getApplicationContext()).load(mPhotoUrl)
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(profileImageView);
}
//TODO: The orientation of views and image is not proper
// [END Set the navigation header details]
}
}
/*@Override
protected void onResume(){
super.onResume();
this.registerReceiver(mSignOutReceiver, signOutFilter);
}*/
@Override
protected void onDestroy(){
super.onDestroy();
this.unregisterReceiver(mSignOutReceiver);
this.unregisterReceiver(mRevokeAccessReceiver);
}
@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.master, 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();
if (id == R.id.action_settings) {
return true;
} else if (id == R.id.sign_out_menu ){
signOutBroadCast();
return true;
} else if (id == R.id.revoke_menu){
revokeAccessBroadCast();
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_logout) {
// 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;
}
private void signOutBroadCast(){
// 1. Clear the shared preference.
editor.clear();
editor.apply();
// 2.Send a sign out broadcast
Intent signOutIntent = new Intent();
signOutIntent.setAction(getString(R.string.action_signout));
sendBroadcast(signOutIntent);
// 3. Start the login Activity
/*Intent signinIntent = new Intent(this,SigninActivity.class);
startActivity(signinIntent);
finish();*/
}
private void revokeAccessBroadCast(){
// 1. Clear the shared preference.
editor.clear();
editor.apply();
// 2.Send a revoke intent.
Intent revokeIntent = new Intent();
revokeIntent.setAction(getString(R.string.action_revoke));
sendBroadcast(revokeIntent);
//signOutBroadCast();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.d(TAG, "onConnectionFailed: "+ connectionResult);
}
}
public class SigninActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener, View.OnClickListener,
GoogleApiClient.ConnectionCallbacks{
private static final String TAG = SigninActivity.class.getSimpleName();
private static final int REQ_ACCPICK = 1;
private static final int RC_SIGN_IN = 2;
private GoogleSignInOptions mGoogleSignInOptions;
private GoogleApiClient mGoogleApiClient;
private SharedPreferences sp;
private SharedPreferences.Editor editor;
private IntentFilter mSignOutFilter;
private BroadcastReceiver mSignOutReceiver;
private IntentFilter mRevokeAccessFilter;
private BroadcastReceiver mRevokeAccessReceiver;
private boolean isAccntConnected;
private SignInButton btnSignIn;
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSignOutFilter = new IntentFilter();
mSignOutFilter.addAction(getString(R.string.action_signout));
mSignOutReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
signOutIfConnected();
Log.d(TAG, "Sign out complete.");
}
};
this.registerReceiver(mSignOutReceiver, mSignOutFilter);
mRevokeAccessFilter = new IntentFilter();
mRevokeAccessFilter.addAction(getString(R.string.action_revoke));
mRevokeAccessReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG,"Revoke access");
revokeAccess();
Log.d(TAG, "Complete access revoked.");
}
};
this.registerReceiver(mRevokeAccessReceiver, mRevokeAccessFilter);
// [START Sign out if connected.]
//signOutIfConnected();
// [END Sign out if connected.]
setContentView(R.layout.activity_signin);
btnSignIn = (SignInButton) findViewById(R.id.btn_sign_in);
btnSignIn.setOnClickListener(this);
// [START Configure sign in]
// configure sign in options for google account
mGoogleSignInOptions= new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
// [END Configure sign in]
isAccntConnected= false;
// [START Build Google api client]
/*mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();*/
// [END Build Google api client]
btnSignIn.setSize(SignInButton.SIZE_STANDARD);
btnSignIn.setScopes(mGoogleSignInOptions.getScopeArray());
sp = getSharedPreferences(getString(R.string.user_cred_sp), MODE_PRIVATE);
editor = sp.edit();
}
/*@Override
protected void onResume(){
super.onResume();
this.registerReceiver(mSignOutReceiver, mSignOutFilter);
this.registerReceiver(mRevokeAccessReceiver, mRevokeAccessFilter);
}*/
/*@Override
protected void onPause(){
super.onPause();
this.unregisterReceiver(mSignOutReceiver);
this.unregisterReceiver(mRevokeAccessReceiver);
}*/
@Override
protected void onDestroy(){
super.onDestroy();
this.unregisterReceiver(mSignOutReceiver);
this.unregisterReceiver(mRevokeAccessReceiver);
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.d(TAG, "onConnectionFailed: " + connectionResult);
}
private void initGAC(){
/*mGoogleSignInOptions= new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.setAccountName(acntName)
.build();*/
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, mGoogleSignInOptions)
//.setAccountName(acntName)
.build();
}
@Override
public void onStart() {
super.onStart();
if(mGoogleApiClient!=null){
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (opr.isDone()) {
// If the user's cached credentials are valid, the OptionalPendingResult will be "done"
// and the GoogleSignInResult will be available instantly.
Log.d(TAG, "Got cached sign in");
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else {
// If the user has not previously signed in on this device or the sign-in has expired,
// this asynchronous branch will attempt to sign in the user silently. Cross-device
// single sign-on will occur in this branch.
showProgressDialog();
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(GoogleSignInResult googleSignInResult) {
hideProgressDialog();
handleSignInResult(googleSignInResult);
}
});
}
}
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.btn_sign_in) {
signIn();
}
}
private void signIn() {
/*startActivityForResult(AccountPicker.newChooseAccountIntent(
null, null, new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE}, true, null, null, null, null
),REQ_ACCPICK);*/
Log.d(TAG, "Sign in method called.");
initGAC();
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);
/*if(requestCode == REQ_ACCPICK){
if(data!=null && data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME)!=null){
mEmail = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
initGAC(mEmail);
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
} else*/
if (requestCode == RC_SIGN_IN) {
Log.d(TAG, "Sign in request");
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Sign in successful. Show authenticated UI
// Set the data in intent and send it to next activity
GoogleSignInAccount acct = result.getSignInAccount();
Log.d(TAG, "Account Profile URL: "+acct.getPhotoUrl().toString());
String userId = acct.getId();
String userDisplayName = acct.getDisplayName();
String userPhotoUrl = acct.getPhotoUrl().toString();
String userEmail = acct.getEmail();
//Set the id in shared preferences so that it can be used to log out
editor.putString("USER_ID", userId);
editor.putString("USER_DISPLAY_NAME", userDisplayName);
editor.putString("USER_PIC_URL", userPhotoUrl);
editor.putString("USER_EMAIL", userEmail);
editor.commit();
//dataIntent.putExtra("USER_EMAIL",userEmail);
Intent dataIntent = new Intent(this, MasterActivity.class);
startActivity(dataIntent);
}
}
private void showProgressDialog() {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage(getString(R.string.loading));
mProgressDialog.setIndeterminate(true);
}
mProgressDialog.show();
}
private void hideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.hide();
}
}
private void signOutIfConnected() {
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
//mEmail=null;
mGoogleApiClient.clearDefaultAccountAndReconnect();
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
Log.d(TAG, "Sign Out using Google Api.");
mGoogleApiClient.disconnect();
isAccntConnected = false;
}
});
}
}
private void revokeAccess(){
if (mGoogleApiClient != null && mGoogleApiClient.isConnected() && isAccntConnected == true) {
Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
Log.d(TAG, "Revoke access using Google Api.");
signOutIfConnected();
}
});
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
this.isAccntConnected = true;
}
@Override
public void onConnectionSuspended(int i) {
}
/*@Override
public void onConnected(@Nullable Bundle bundle) {
if(!sp.contains("USER_ID_TOKEN")){
signOut();
}
}
@Override
public void onConnectionSuspended(int i) {
}*/
}
public class SigninActivity扩展AppCompativity实现GoogleAppClient.OnConnectionFailedListener、View.OnClickListener、,
GoogleAppClient.ConnectionCallbacks{
private static final String TAG=SigninActivity.class.getSimpleName();
专用静态最终整型请求_ACCPICK=1;
专用静态最终int RC_SIGN_IN=2;
私人谷歌签名mGoogleSignInOptions;
私人GoogleapClient MGoogleapClient;
私人共享参考sp;
私有共享参考。编辑器;
私有IntentFilter mSignOutFilter;
专用广播接收机mSignOutReceiver;
私有IntentFilter mRevokeAccessFilter;
专用广播接收机mrevokaccessreceiver;
私有布尔是连通的;
私人签名按钮;
private ProgressDialog mProgressDialog;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mSignOutFilter=newintentfilter();
mSignOutFilter.addAction(getString(R.string.action_signout));
mSignOutReceiver=新广播接收器(){
@凌驾
公共void onReceive(上下文、意图){
signOutIfConnected();
Log.d(标记“完成注销”);
}
};
此.registerReceiver(mSignOutReceiver,mSignOutFilter);
mRevokeAccessFilter=新的IntentFilter();
mRevokeAccessFilter.addAction(getString(R.string.action_revoke));
mRevokeAccessReceiver=新广播接收器(){
@凌驾
公共void onReceive(上下文、意图){
Log.d(标记“撤销访问”);
撤销访问();
Log.d(标签“撤销完全访问权”);
}
};
此.registerReceiver(mRevokeAccessReceiver,mRevokeAccessFilter);
//[如果已连接,则开始注销。]
//signOutIfConnected();
//[如果已连接,则结束注销。]
setContentView(R.layout.activity\u signin);
btnSignIn=(签名按钮)findViewById(R.id.btn\u sign\u in);
btnSignIn.setOnClickListener(此);
//[开始配置登录]
//为google帐户配置登录选项
mGoogleSignInOptions=新的GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT\u SIGN\u IN)
.requestEmail()
.build();
//[结束配置登录]
isAccntConnected=false;
//[开始构建Google api客户端]
/*mgoogleapclient=新的Googleapclient.Builder(此)
.enableAutoManage(这个,这个)
.addApi(Auth.GOOGLE\u SIGN\u IN\u API,gso)
.build()*/
//[最终构建Google api客户端]
btnSignIn.setSize(标志按钮尺寸标准);
设置范围(mGoogleSignInOptions.getScopeArray());
sp=getSharedReferences(getString(R.string.user\u cred\u sp),MODE\u PRIVATE);
editor=sp.edit();
}
/*@凌驾
受保护的void onResume(){
super.onResume();
此.registerReceiver(mSignOutReceiver,mSignOutFilter);
此.registerReceiver(mRevokeAccessReceiver,mRevokeAccessFilter);
}*/
/*@凌驾
受保护的void onPause(){
super.onPause();
此。未注册接收人(mSignOutReceiver);
这是未注册的接收方(mrevokaccessreceiver);
}*/
@凌驾
受保护的空onDestroy(){
super.ondestory();
此。未注册接收人(mSignOutReceiver);
这是未注册的接收方(mrevokaccessreceiver);
}
@凌驾
public void onconnection失败(@NonNull ConnectionResult ConnectionResult){
Log.d(标记“onConnectionFailed:+connectionResult”);
}
私有void initGAC(){
/*mGoogleSignInOptions=新的GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT\u SIGN\u IN)
.setAccountName(acntName)
.build()*/
mgoogleapclient=新的Googleapclient.Builder(此)
.enableAutoManage(这个,这个)
.addApi(Auth.GOOGLE\u SIGN\u IN\u API,mGoogleSignInOptions)
//.setAccountName(acntName)
.build();
}
@凌驾
public void onStart(){
super.onStart();
if(mGoogleApiClient!=null){
OptionalPendingreult opr=Auth.GoogleSignInApi.silentSignIn(MGoogleAppClient);
if(opr.isDone()){
//如果用户的缓存凭据有效,则OptionalPendingreult将“完成”
//谷歌搜索结果将立即发布。
Log.d(标记“已缓存登录”);
GoogleSignInResult结果=操作获取();
handleSignInResult(结果);
}否则{
//如果用户以前未在此设备上登录或登录已过期,
//此异步分支将尝试以静默方式登录用户。跨设备
//此分支中将发生单点登录。
showProgressDialog();
操作setResultCallback(新的ResultCallback(){
@凌驾
公开作废结果(GoogleSignInResult GoogleSignInResult){
hideProgressDialog();
handleSignInResult(谷歌SignInResult);
}
});
}
}
}
@凌驾
公共void onClick(视图v){
int id=v.getId();
if(id==R.id.btn\u登录){
签名();
}
}
私人无效签名(){
/*startActivityForResult(AccountPicker.newChooseAccountContent(
null,null,新字符串[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE},true,null,null,null,null
),要求(选择)*/
d(标记“已调用的登录方法”);
initGAC();
我
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN && resultCode == RESULT_OK) {
Log.d(TAG, "Sign in request");
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
mGoogleApiClient.connect();//CALL TO CONNECT GoogleApiClient
handleSignInResult(result);
}
}
private void signOutIfConnected() {
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.clearDefaultAccountAndReconnect();
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
Log.d(TAG, "Sign Out using Google Api.");
mGoogleApiClient.disconnect();
//CALL TO DISCONNECT GoogleApiClient
isAccntConnected = false;
}
});
}
}
private void revokeAccess(){
if (isAccntConnected == true) {
Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
Log.d(TAG, "Revoke access using Google Api.");
//signOutIfConnected(); //REMOVED
}
});
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
isAccntConnected = true;
}