Android Question reading an email account with Android 8.0

pesquera

Active Member
Licensed User
Longtime User
Hi everyone,

I have been used this code for a long time without any issue.. now, I have an error on Android 8

The code:
B4X:
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")
r.Target = accounts(0) ' read the first record
loc_ToRetun = r.GetField("name")


The app crash on this line:
B4X:
r.Target = accounts(0) ' read the first record


The debug:
B4X:
Error occurred on line: 267 (zzUtil)[/COLOR][/FONT][/LEFT]
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
 at java.lang.reflect.Array.get(Array.java:174)
 at anywheresoftware.b4a.shell.ArraysUtils.getElement(ArraysUtils.java:76)
 at anywheresoftware.b4a.shell.Shell.getArrayElement(Shell.java:568)
 at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:374)
 at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
 at java.lang.reflect.Method.invoke(Native Method)
 at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
 at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:259)
 at agendame.turnos.zzutil._rtn_sendpushtohabilitador(zzutil.java:2586)
 at agendame.turnos.zzutil._rtn_catcherror(zzutil.java:412)
 at agendame.turnos.zzutil._fnc_agendaidfromnro(zzutil.java:8997)
 at agendame.turnos.zzutil._rtn_generar_userdatafile(zzutil.java:7721)
 at agendame.turnos.act_soporte._mibuttonenviarlog_click(act_soporte.java:445)
 at agendame.turnos.act_soporte._ab_btn_enviarlog_click(act_soporte.java:413)
 at java.lang.reflect.Method.invoke(Native Method)
 at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
 at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
 at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
 at java.lang.reflect.Method.invoke(Native Method)
 at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
 at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1058)
 at anywheresoftware.b4a.keywords.Common.CallSubNew3(Common.java:1021)
 at agendame.turnos.clsactionbar._button_click(clsactionbar.java:1290)
 at java.lang.reflect.Method.invoke(Native Method)
 at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
 at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
 at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
 at java.lang.reflect.Method.invoke(Native Method)
 at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
 at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
 at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
 at android.view.View.performClick(View.java:6294)
 at android.view.View$PerformClick.run(View.java:24770)
 at android.os.Handler.handleCallback(Handler.java:790)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:164)
 at android.app.ActivityThread.main(ActivityThread.java:6494)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Could it be related to permissions? Witch one?

Thanks all
 

DonManfred

Expert
Licensed User
Longtime User
https://developer.android.com/about/versions/oreo/android-8.0-changes

Account access and discoverability
In Android 8.0 (API level 26), apps can no longer get access to user accounts unless the authenticator owns the accounts or the user grants that access. The GET_ACCOUNTS permission is no longer sufficient. To be granted access to an account, apps should either use AccountManager.newChooseAccountIntent() or an authenticator-specific method. After getting access to accounts, an app can can call AccountManager.getAccounts() to access them.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
How did you get this working. I tried Erel's code and it crashes.

Need to verify the user has an Email Account setup


Found this code by Erel that gets me the Emails

B4X:
  Dim r As Reflector
  Dim wAccounts As Map
  wAccounts.Initialize
  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.RunMethod("getAccounts")
  For i = 0 To accounts.Length - 1
  r.Target = accounts(i)
      wAccounts.Put(r.GetField ("name"), "")
  Next
  For Each account As String In wAccounts.Keys
     Log(account)
   Next

The only problem is it always seems to return just 1 Email and I have multiple
 
Last edited:
Upvote 0
Top