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

Discussion in 'Android Questions' started by Serway, Feb 23, 2018.

  1. Serway

    Serway Member Licensed 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 (?) :

    Code:
    public void pickUserAccount() {
        /*This will 
    list all available accounts on device without any filtering*/
        
    Intent intent = AccountPicker.newChooseAccountIntent(nullnullnullfalsenullnullnullnull);
        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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    Code:
    Sub ShowPicker
       
    Dim AccountPicker As JavaObject
       AccountPicker.InitializeStatic(
    "com/google/android/gms/common/AccountPicker".Replace("/""."))
       
    Dim i As Intent = AccountPicker.RunMethod("newChooseAccountIntent"Array(NullNullArray As String("com.google"), _
           
    FalseNullNullNullNull))
       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:
    Code:
    #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/
     
    pesquera, Pencil3, Serway and 4 others like this.
  3. Serway

    Serway Member Licensed User

    Thank you Erel
    :)
     
  4. pesquera

    pesquera Active Member Licensed 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!
     
  5. DonManfred

    DonManfred Expert Licensed 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?
     
  6. pesquera

    pesquera Active Member Licensed 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
     
  7. Robert Valentino

    Robert Valentino Well-Known Member Licensed 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: Jul 2, 2018
  8. Erel

    Erel Administrator Staff Member Licensed User

    Are you testing it on a real device?
     
  9. Robert Valentino

    Robert Valentino Well-Known Member Licensed User

    Yes. Samsung S7 just upgraded to Android 8.0
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Are you using B4A Sdk Manager?
     
  11. Robert Valentino

    Robert Valentino Well-Known Member Licensed 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
     
    Erel likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice