Android Question How to translate java (AccountPicker) to B4A ...

Serway

Member
Licensed User
Longtime User
Hello,

I'm trying to get user email registered account without the GET_ACCOUNTS permission (android.permission.GET_ACCOUNTS).

Is anyone can help to translate into B4A (?) :

B4X:
public void pickUserAccount() {
    /*This will list all available accounts on device without any filtering*/
    Intent intent = AccountPicker.newChooseAccountIntent(null, null, null, false, null, null, null, null);
    startActivityForResult(intent, REQUEST_CODE_PICK_ACCOUNT);
}
 /*After manually selecting every app related account, I got its Account type using the code below*/
 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_PICK_ACCOUNT) {
        // Receiving a result from the AccountPicker
        if (resultCode == RESULT_OK) {
            System.out.println(data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE));
            System.out.println(data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME));
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, R.string.pick_account, Toast.LENGTH_LONG).show();
        }
    }
}

Source : https://developers.google.com/android/reference/com/google/android/gms/common/AccountPicker

Thanks,
Thierry
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Code:
B4X:
Sub ShowPicker
   Dim AccountPicker As JavaObject
   AccountPicker.InitializeStatic("com/google/android/gms/common/AccountPicker".Replace("/", "."))
   Dim i As Intent = AccountPicker.RunMethod("newChooseAccountIntent", Array(Null, Null, Array As String("com.google"), _
       False, Null, Null, Null, Null))
   StartActivityForResult(i)
End Sub


Sub ion_Event (MethodName As String, Args() As Object) As Object
   If Args(0) = -1 Then 'resultCode = RESULT_OK
       Dim data As Intent = Args(1)
       Dim StaticAccountManager As JavaObject
       StaticAccountManager.InitializeStatic("android.accounts.AccountManager")
       Log(data.ExtrasToString)
       Dim Name As String = data.GetExtra(StaticAccountManager.GetField("KEY_ACCOUNT_NAME"))
       Log(Name)
   End If
   Return Null
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

Add:
B4X:
#AdditionalJar: com.google.android.gms:play-services-base
Add google play services base manifest snippet: https://www.b4x.com/android/forum/threads/integrating-firebase-services.67692/
 
Upvote 0

pesquera

Active Member
Licensed User
Longtime User
Hi,

I just need to read the first account.. I don´t need to pick from the accounts

I have a routine into a class, called from different routines along the app
Is it possible to adapt this code for my needs?

Thanks in advance!
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I quoted a bit of the documentation in the other thread.
If i remember correctly the quote states that you need to get the access one time using this method. After you got the permission you are allowed to use GET_ACCOUNT.
Did i read that wrong?
 
Upvote 0

pesquera

Active Member
Licensed User
Longtime User
Sorry for mix the threads, I thought that here was the solution I was looking for.. I will continue on the another thread.. excuse me for the inconvenience
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
in Erel's code where is Ion defined and defined as what

and when I execute the AccountsPicker.InitializeStatic I get

Error occurred on line: 1656 (Main)
java.lang.ClassNotFoundException: com.google.android$gms$common$AccountPicker
at anywheresoftware.b4j.object.JavaObject.getCorrectClassName(JavaObject.java:288)
at anywheresoftware.b4j.object.JavaObject.InitializeStatic(JavaObject.java:74)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:780)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:363)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:260)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA$2.run(BA.java:365)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

And I do have AdditionalJar statement
 
Last edited:
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I am. But just made sure everything is up to date including tools and it seems to be working.

Sorry should be better at keeping everything updated... But it is summer LOL
 
Upvote 0

Similar Threads

Top