Android项目的应用默认凭据应如何配置?
创始人
2024-10-12 13:01:26
0

在Android项目中使用Google提供的GoogleApiClient类,可以轻松获取应用程序默认凭据。以下是示例代码:

  1. 在项目中添加以下依赖项:
dependencies {
  implementation 'com.google.android.gms:play-services-auth:16.0.1'
}
  1. 在Application类中构建GoogleApiClient:
public class MyApp extends Application implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {
 
    private static final String TAG = MyApp.class.getSimpleName();
    private GoogleApiClient mGoogleApiClient;
 
    @Override
    public void onCreate() {
        super.onCreate();
        //创建GoogleApiClient
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */ , this /* OnConnectionFailedListener */ )
                .addConnectionCallbacks(this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
                .build();
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        Log.i(TAG, "GoogleApiClient Connected!");
 
        // 获取默认的凭据
        GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(getApplicationContext());
        if (account != null) {
            String name = account.getDisplayName();
            String email = account.getEmail();
            String id = account.getId();
 
            Log.i(TAG, "name:" + name);
            Log.i(TAG, "email:" + email);
            Log.i(TAG, "id:" + id);
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.i(TAG, "GoogleApiClient Suspended!");
    }
 
    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.i(TAG, "GoogleApiClient Connection Failed!");
    }
}
  1. 在需要获取凭据的Activity中获取GoogleApiClient实例,并添加相应的连接回调:
public class MainActivity extends AppCompatActivity {

    private GoogleApiClient mGoogleApiClient;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        // 获取GoogleApiClient实例
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)

相关内容

热门资讯

透视辅助"德扑之心免... 透视辅助"德扑之心免费透视"详细辅助安装教程(开脚本视频透视);支持2-10人实时对战,虚拟庄家发牌...
透视软件"aapok... 透视软件"aapoker透视方法"详细辅助可靠技巧(可以下载游戏技巧)科技教程也叫必备教程,这是一款...
透视辅助"xpoke... 这是一款非常优秀的xpoker辅助神器 ia辅助检测软件,能够让你了解到xpoker辅助神器中牌率当...
透视ai代打"佛手在... 透视ai代打"佛手在线大菠萝为什么都输"详细辅助wepoke教程(轻量版辅助方法);原来确实真的有挂...
透视好友房"hhpo... 透视好友房"hhpoker真的假的"详细辅助透视教程(可以看底牌透明);原来确实真的有挂(需添加指定...
透视软件"wepok... 您好,wepokerplus开挂这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】...
透视最新"红龙pok... 透视最新"红龙poker作弊指令"详细辅助曝光教程(可以看牌型方法);人气非常高,ai更新快且高清可...
透视总结"wepok... 自定义wepoker辅助分析器系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助...
透视挂"德州透视是真... 自定义德州透视是真的吗系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不管...
透视安装"佛手在线有... 透视安装"佛手在线有挂吗"详细辅助微扑克教程(辅助器教程)是由北京得佛手在线有挂吗黑科技有限公司精心...