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)

相关内容

热门资讯

透视能赢!德普之星辅助工具如何... 透视能赢!德普之星辅助工具如何打开,德普之星辅助器怎么用(透视)确实真的是有挂(证实有挂)-哔哩哔哩...
这一问题亟待解决!pokerw... 这一问题亟待解决!pokerworld辅助器,新海贝之城脚本,操作教程(今日头条)-哔哩哔哩新海贝之...
透视教程!德扑圈透视,德扑圈有... 透视教程!德扑圈透视,德扑圈有透视吗(透视)切实存在有挂(有挂教程)-哔哩哔哩1、进入游戏-大厅左侧...
此事引发网友热议!天天贵州万能... 此事引发网友热议!天天贵州万能辅助器,pokemmo辅助器脚本下载,阶段教程(有挂技巧)-哔哩哔哩p...
透视教学!德普软件,德普之星透... 您好,德普之星透视辅助这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩...
据悉!微乐麻将脚本透视,广东雀... 据悉!微乐麻将脚本透视,广东雀神智能插件是真有挂,操作教程(有挂分享)-哔哩哔哩所有人都在同一条线上...
透视规律!德普之星透视辅助软件... 透视规律!德普之星透视辅助软件,德普之星透视免费(透视)都是真的有挂(有挂教学)-哔哩哔哩暗藏猫腻,...
今天上午!逸趣鄱阳翻精辅助,w... 今天上午!逸趣鄱阳翻精辅助,wepoker正确养号方法,总结教程(有挂解惑)-哔哩哔哩1、每一步都需...
透视总结!德普之星透视辅助软件... 透视总结!德普之星透视辅助软件激活码,德普之星透视辅助软件激活码(透视)其实真的有挂(有挂存在)-哔...
有了最新消息!新星游挂多少钱,... 有了最新消息!新星游挂多少钱,wpk模拟器是什么,攻略教程(有挂教学)-哔哩哔哩1、玩家可以在wpk...