Android Question How to get the phone owners profile?

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code to see all the data:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim cr As ContentResolver
   Dim u As Uri
   u.Parse("content://com.android.contacts/profile")
   cr.Initialize("cr")
   Dim crsr As Cursor = cr.Query(u, Null, "", Null, "")
   printCursor(crsr)
End Sub

Private Sub printCursor(c As Cursor) 
   For r = 0 To c.RowCount - 1
     c.Position = r
     For col = 0 To c.ColumnCount - 1
       Log(c.GetColumnName(col) & ": " & c.GetString2(col))
     Next
   Next
End Sub
Depends on ContentResolver and SQL libraries.

You need to add these permissions to the manifest editor:
B4X:
AddPermission("android.permission.READ_CONTACTS")
AddPermission("android.permission.READ_PROFILE")
 
Upvote 0

mmieher

Active Member
Licensed User
Longtime User
You can use this code to see all the data:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim cr As ContentResolver
   Dim u As Uri
   u.Parse("content://com.android.contacts/profile")
   cr.Initialize("cr")
   Dim crsr As Cursor = cr.Query(u, Null, "", Null, "")
   printCursor(crsr)
End Sub

Private Sub printCursor(c As Cursor)
   For r = 0 To c.RowCount - 1
     c.Position = r
     For col = 0 To c.ColumnCount - 1
       Log(c.GetColumnName(col) & ": " & c.GetString2(col))
     Next
   Next
End Sub
Depends on ContentResolver and SQL libraries.

You need to add these permissions to the manifest editor:
B4X:
AddPermission("android.permission.READ_CONTACTS")
AddPermission("android.permission.READ_PROFILE")

Erel --

I get an error the the u Uri first needs to be initialized. Cannot see how to do that?

Marc
 
Upvote 0

mmieher

Active Member
Licensed User
Longtime User
[QUOTE="

Not seeing anything in the logs. When I run the App, I get "Error occurred. An error has ocurred in sub: java.lang.RuntimeException: Object should firstr be initialized (Uri). Continue?".


Sub GetEmailAddress

Dim r As Reflector
Dim AccountName As String
Dim LineNumber As String

Dim cr As ContentResolver
Dim u As Uri
Dim c As Cursor = cr.Query(u, Null, "", Null, "")

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)
AccountName = r.GetField("name")

InstallEMail = AccountName

Next

' ---------------------------------------------------------------

r.Target = r.GetContext
r.Target = r.RunMethod2("getSystemService", "phone", "java.lang.String")
LineNumber = r.RunMethod ("getLine1Number")

InstallPhone = LineNumber

'---------------------------------------------------------------

u.Parse("content://com.android.contacts/profile")
cr.Initialize("cr")

For i = 0 To c.RowCount - 1
c.Position = r
For col = 0 To c.ColumnCount - 1

InstallName = c.GetColumnName(col) & ": " & c.GetString2(col)

' Log(c.GetColumnName(col) & ": " & c.GetString2(col))


Next
Next

Msgbox(InstallName,"")


End Sub
 
Upvote 0

Anser

Well-Known Member
Licensed User
Longtime User
Are you trying it on a real phone or emulator ?
I believe that it will not return any values if you run it on an emulator
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
Thanks @Erel!
I runned your code and it works fine. Is it possible to have also the android owner account (GMAIL) ?
As PrintCursor output I can see the Full owner name but not the owner gmail.
 
Upvote 0
Top