sub getAccountEmail
If sdkVersion < 23 Then '< Android 6 - permission already granted in manifest file
userEmail = getAccountsForOlderDevices.Get(0)
else If sdkVersion < 26 Then 'Android 6-7 - need runtime permission
warnAboutGetAccountPermission
Else '>= Android 8 - use account picker (no permission needed)
warnAboutPickingAccount
End If
end sub
'https://www.b4x.com/android/forum/threads/owner-accounts-with-all-field.41101/
'for devices less than Android 8, return a list of account email addresses. (newer devices get an empty list)
Sub getAccountsForOlderDevices As List
Dim wAccounts As List
wAccounts.Initialize
If sdkVersion < 26 Then
Dim r As Reflector
r.Target = r.RunStaticMethod("android.accounts.AccountManager", "get", Array As Object(r.GetContext), Array As String("android.content.Context"))
Dim accounts() As Object
accounts = r.RunMethod2("getAccountsByType", "com.google", "java.lang.String")
For i = 0 To accounts.Length - 1
r.Target = accounts(i)
wAccounts.Add(r.GetField("name")) 'actually email address
Next
Else
LogColor("getAccounts: empty list for Android 8+", Colors.Yellow)
End If
Return wAccounts
End Sub
'check/request permission, then get their email from their Google account on this device
Sub warnAboutGetAccountPermission
MsgboxAsync("This app needs your permission to get your account's email for sharing.", "Heads up!")
wait for Msgbox_Result(result As Int)
askAndGetAccountInfo
End Sub
Sub askAndGetAccountInfo
rp.CheckAndRequest(c.rp.PERMISSION_GET_ACCOUNTS)
wait for Activity_PermissionResult(Permission As String, result As Boolean)
If result = True Then
userEmail = getAccountsForOlderDevices.Get(0)
Else
MsgboxAsync("Sorry, can't continue without your account details", "No sharing")
End If
End Sub
'ask the user to pick their Google account, then get their name and email from their Google account on this device
Sub warnAboutPickingAccount
MsgboxAsync("Please pick your current account so the app can get your email address.", "Heads up!")
wait for Msgbox_Result(result As Int)
pickAccountToGetEmail
End Sub
'https://www.b4x.com/android/forum/threads/get-email-accounts-returns-empty-list.105275/post-659474
Sub pickAccountToGetEmail
Dim jo As JavaObject
jo.InitializeStatic("android.accounts.AccountManager")
StartActivityForResult(jo.RunMethod("newChooseAccountIntent", Array(Null, Null, Array As String("com.google"), Null, Null, Null, Null)))
Wait For ion_Event(MethodName As String, Args() As Object)
If -1 = Args(0) Then 'resultCode = RESULT_OK
Dim i As Intent = Args(1)
userEmail = i.GetExtra("authAccount") 'email address of account
Else
userEmail = ""
MsgboxAsync("Sorry, could not get your account details", "Error")
End If
End Sub
Sub StartActivityForResult(i As Intent)
Dim jo As JavaObject = GetBA
ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub
Sub GetBA As Object
Dim jo As JavaObject
Dim cls As String = Me
cls = cls.SubString("class ".Length)
jo.InitializeStatic(cls)
Return jo.GetField("processBA")
End Sub