简体   繁体   English

以编程方式将 Google 帐户注册到 Android 设备

[英]Registering a Google account to Android device programmatically

I've been looking for a solution to this problem for a while (days, not minutes), but it eludes me quite effectively.我一直在寻找解决这个问题的方法有一段时间(几天,而不是几分钟),但它非常有效地避开了我。

Please note that this is NOT a question about starting up the registration procedure.请注意,这不是关于启动注册程序的问题。 This must happen automatically without any user interaction.这必须在没有任何用户交互的情况下自动发生。

I would like to add a Google account to my custom device (1000's of them).我想在我的自定义设备(其中 1000 个)中添加一个 Google 帐户。 The account will mostly be used to activate Google Play store on the device so that the app can update when newer versions are available.该帐户主要用于激活设备上的 Google Play 商店,以便在有新版本可用时更新应用程序。

My existing code (the shortest snippet of those I tried):我现有的代码(我尝试过的最短代码段):

AccountManager mgr = AccountManager.get(this);
Account acc = new Account("email@gmail.com", "com.google");
mgr.addAccountExplicitly(acc, "password", new Bundle()));

naturally yields a自然产生一个

java.lang.SecurityException: caller uid 10047 is different than the authenticator's uid

So how would I go about actually achieving this?那么我将如何真正实现这一目标? My device is rooted so that's not an obstacle if it's the only way.我的设备已植根,因此如果这是唯一的方法,那不是障碍。

Warning: this solution doesn't work well.警告:此解决方案效果不佳。 See comments for explanation.请参阅注释以获取解释。

Well, as it turns out, this is not something easily solved.嗯,事实证明,这不是一件容易解决的事情。 I ended up registering one device, then pulled the users file from it.我最终注册了一台设备,然后从中提取了用户文件。 Location of users file : /data/system/users/0/accounts.db (if there are multiple user profiles on the device, the last directory may differ according to profile in question).用户文件的位置: /data/system/users/0/accounts.db (如果设备上有多个用户配置文件,最后一个目录可能会根据相关配置文件的不同而不同)。

I stored this file into my app's assets (gzipped, make sure the extension is not something.gz because that gets lost during packaging - didn't bother checking out why).我将此文件存储到我的应用程序的资产中(gzipped,确保扩展名不是 something.gz 因为它在打包过程中丢失了 - 没有费心检查原因)。

First I check if my user already exists:首先我检查我的用户是否已经存在:

AccountManager mgr = AccountManager.get(this);
for (Account acc: mgr.getAccountsByType("com.google")) {
  if (acc.name.equalsIgnoreCase("email@gmail.com"))
    return;
}

If it does, I just skip the step.如果是这样,我只是跳过这一步。 Otherwise I unpack the users file and overwrite existing one (using su ).否则我解压用户文件并覆盖现有文件(使用su )。 I then also do a reboot to make sure changes are registered.然后我还重新启动以确保更改已注册。

It is not possible to add/create a Google account using addAccountExplicitly().无法使用 addAccountExplicitly() 添加/创建 Google 帐户。 You can only add accounts for your own services.您只能为自己的服务添加帐户。 even your device is rooted because it will rejected by Google web server.甚至您的设备已植根,因为它会被谷歌网络服务器拒绝。 For more detail check this link有关更多详细信息,请查看此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM