推送通知android GCM

时间:2014-03-14 13:33:41

标签: php android

我是Android应用的初学者。

我找到this tutorial并且它有效,但权限是GET_ACCOUNTS,当运行应用时,会显示名称和电子邮件的显示输入。如何在没有输入姓名和电子邮件的情况下进行更改,只需从GET_ACCOUNTS开始?我不明白。任何人都可以解释一下吗?

1 个答案:

答案 0 :(得分:1)

在我上一次的apk中,我遇到了同样的问题。但我在This回答中找到了。

我希望它有用

在清单中检查权限:

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

在方法中:

public String getEmail(){
    String possibleEmail= "no email";
    Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
    Account[] accounts = AccountManager.get(ctx).getAccounts();
    for (Account account : accounts) {
        if (emailPattern.matcher(account.name).matches()) {
            possibleEmail = account.name;              
        }
    }

    return possibleEmail;

}

在示例中更改为:

protected void onRegistered(Context context, String registrationId) {
    Log.i(TAG, "Device registered: regId = " + registrationId);
    displayMessage(context, "Your device registred with GCM");
    Log.d("NAME", MainActivity.name);
    ServerUtilities.register(context, MainActivity.name, getEMail(), registrationId);
}