gpt4 book ai didi

android - 如何从google plus中的google plus帐户注销以及android中的集成

转载 作者:太空狗 更新时间:2023-10-29 12:40:47 26 4
gpt4 key购买 nike

我做了以下工作,但在注销后它无法正常工作再次快速登录应用程序

private void signOutFromGplus() {

if (mGoogleApiClient.isConnected()) {

// clearCookies();

Plus.AccountApi
.clearDefaultAccount(mGoogleApiClient);

.mGoogleApiClient.disconnect();

.mGoogleApiClient.connect();
}
}

最佳答案

在这个类中为你分配变量(使用这个类的好处是这个类在应用程序启动时启动,你可以在应用程序的任何地方访问它)如下:MyApplication.class

import android.app.Application;
import android.support.v7.app.AppCompatActivity;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.GoogleApiClient;
public class MyApplication extends Application {
private GoogleApiClient mGoogleApiClient;
private GoogleSignInOptions gso;
public AppCompatActivity activity;
public GoogleSignInOptions getGoogleSignInOptions(){
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
return gso;
}
public GoogleApiClient getGoogleApiClient(AppCompatActivity activity, GoogleApiClient.OnConnectionFailedListener listener){
this.activity = activity;
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this.activity, listener)
.addApi(Auth.GOOGLE_SIGN_IN_API, getGoogleSignInOptions())
.build();
return mGoogleApiClient;
}
}

在登录 Activity 中

    @Override
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (opr.isDone()) {
Log.d("TAG", "Got cached sign-in");
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else {
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(GoogleSignInResult googleSignInResult) {
//hideProgressDialog();
handleSignInResult(googleSignInResult);
}
});
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

gso = ((MyApplication) getApplication()).getGoogleSignInOptions();
mGoogleApiClient = ((MyApplication) getApplication()).getGoogleApiClient(LoginActivity.this, this);
}
@Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}

在您要注销的第二个 Activity 中

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

mGoogleApiClient = ((MyApplication) getApplication()).getGoogleApiClient(HomeActivity.this, this);

btnLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
Intent i = new Intent(HomeActivity.this, LoginActivity.class);
startActivity(i);
finish();
}
});
}
});
}

在您的应用级 build.gradle 文件中,将 Google Play 服务声明为依赖项:

dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'com.google.gms:google-services:1.5.0-beta2'

让 MyApplication 类扩展默认的 Application 类。另外,打开 Manifest 文件并在应用程序元素中添加这行代码。

android:name=".MyApplication"

关于android - 如何从google plus中的google plus帐户注销以及android中的集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26437445/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com