Hi all,
I recently needed to check for device's google account and I was not able to get it done with Erel's code. I was not getting any results. So a little search on the Internet and a little mixing of the soup and voila:
(it was created for activities but it may be used without any problems - I suppose - also in services)
DO NOT FORGET (AS I DID) TO INCLUDE IN YOUR MANIFEST THE FOLLOWING:
DO NOT FORGET ALSO (AS I DID) TO INCLUDE THESE RUN TIME PERMISSIONS:
Cheers
I recently needed to check for device's google account and I was not able to get it done with Erel's code. I was not getting any results. So a little search on the Internet and a little mixing of the soup and voila:
(it was created for activities but it may be used without any problems - I suppose - also in services)
B4X:
Sub GetPrimaryAccount As String
Dim r As Reflector
Dim sRet As String
Dim j As JavaObject
j.InitializeContext
sRet = j.RunMethod("getEmailID", Array As Object(r.GetContext))
Return sRet
End Sub
#If JAVA
import android.content.Context;
import android.accounts.AccountManager;
import android.accounts.Account;
public String getEmailID(Object objcntx) {
Context context = (Context) objcntx;
AccountManager accountManager = AccountManager.get(context);
Account account = getAccount(accountManager);
if (account == null) {
return null;
} else {
return account.name;
}
};
public Account getAccount(AccountManager accountManager) {
Account[] accounts = accountManager.getAccountsByType("com.google");
Account account;
if (accounts.length > 0) {
account = accounts[0];
// account = accounts[accounts.length-1]; ‘για τον τελευταίο=πρώτος μπήκε
} else {
account = null;
}
return account;
};
#End If
DO NOT FORGET (AS I DID) TO INCLUDE IN YOUR MANIFEST THE FOLLOWING:
B4X:
AddPermission("android.permission.GET_ACCOUNTS")
AddPermission("android.permission.READ_CONTACTS")
DO NOT FORGET ALSO (AS I DID) TO INCLUDE THESE RUN TIME PERMISSIONS:
B4X:
rp.CheckAndRequest(rp.PERMISSION_GET_ACCOUNTS)
rp.CheckAndRequest(rp.PERMISSION_READ_CONTACTS)
Cheers
Last edited: