本文介绍了如何获取Android手机用户的Google帐户名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在用统一引擎制作手机游戏,想发布到Android目标上.

So I am making mobile game with unity engine, think to release to android target.

现在我的游戏将有我自己的自定义排行榜,我认为当用户获得分数时,游戏应该将其身份昵称,分数发送到服务器.

Now my game will have my own custom highscore board, there I think when user earn score, game should send his identity nickname, score to server.

因此,为了获得他的独特名字,我想获得他的google帐户名称.(当然,我可以选择接收用户输入的昵称的选项,但是很累)

So for get his distinct name, I think to get his google account name. (of course I can choose option that receive user's input for his nickname, but well, tiresome)

我只需要电子邮件格式的前半部分(即用户名)例如,如果用户的Google帐户是[email protected],我只希望提取"asdf".

I just only need email format's front part (i.e. username)Example, if user's google account is [email protected] , I want to extract only "asdf".

那么,怎么做?

这是获得他独特昵称的好方法吗?

And is this good way for get his distinct nickname?

谢谢.

所以我确实喜欢这样,但是不起作用.

So I did like this, but does not work.

public class MainActivity extends UnityPlayerActivity implements        AndroidInterface{
private String usergoogleName;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GetUserName();
}
public void GetUserName(){
    AccountManager manager = AccountManager.get(this);
    Account[] accounts = manager.getAccountsByType("com.google");
    List<String> username = new LinkedList<String>();

    for (Account account : accounts) {
        username.add(account.name);
    }
    usergoogleName = username.get(0);
    sendMessage("Main Camera", "JavaMsg", usergoogleName);
}

@Override
public void sendMessage(String target, String method, String params) {
    // TODO Auto-generated method stub
    UnityPlayer.UnitySendMessage(target, method, params);
}

在统一方面,我将脚本附加到了主摄像机"对象上并编写,

and at unity side, I attached script to 'Main Camera' object and write,

public string name;
public void JavaMsg(string str){
    name = str;
}
void OnGUI(){
    GUI.Label(new Rect(100, 200, 300, 100), "google account name is " + name);
}

但是当我在制作.apk文件后在手机上运行此文件时,无法读取帐户名.

but when I run this at my phone after make .apk file, doesn't read account name.

怎么了?

推荐答案

尝试一下

AccountManager manager = AccountManager.get(context);
Account[] accounts = manager.getAccountsByType("com.google");
List<String> username = new LinkedList<String>();

for (Account account : accounts) {
    username.add(account.name);
}

并向Android清单添加权限

and add permission to android manifest

<uses-permission android:name="android.permission.GET_ACCOUNTS" />

这篇关于如何获取Android手机用户的Google帐户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 07:03