B4A Library [library] ContentResolver

ContentResolver library allows you to access "content providers" applications.

This functionality is already available with objects such as: CallLog, Contacts, Contacts2 and others.

With this library you have more flexibility as you can implement it completely in Basic4android.

The classes in this library are similar to the Java classes. The purpose it to make it easier to convert Java code. Note that most of the constants are not available in this wrapper. This means that you need to replace them with the actual values.

The features of ContentResolver are similar to SQL features. The main operations are: Query, Insert, Update and Delete. For each feature there is a synchronous method and an asynchronous method (QueryAsync, InsertAsync...). The asynchronous methods raise events when the operation completes.

Note that you should reference SQL library together with this library.

For example, the following code allows you to access the contacts provider:

Manifest editor

AddPermission("android.permission.READ_CONTACTS")
AddPermission("android.permission.WRITE_CONTACTS")

Code

B4X:
Sub Process_Globals
   Private PeopleProjection() As String = Array As String("times_contacted", "last_time_contacted", _
      "display_name", "has_phone_number", "starred", "_id", "photo_id")
   Private cr As ContentResolver
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      cr.Initialize("cr")
   End If
   UpdateStarred("John", True)
End Sub
'Gets all contacts asynchronously and prints them.
Sub GetAllContacts
   Dim u As Uri
   u.Parse("content://com.android.contacts/contacts")
   cr.QueryAsync(u, PeopleProjection, "", Null, "")
End Sub

Sub CR_QueryCompleted (Success As Boolean, Crsr As Cursor)
   If Success = False Then
      Log(LastException)
   Else
      For i = 0 To Crsr.RowCount - 1
         Crsr.Position = i
         Log(Crsr.GetString("display_name"))
      Next
      Crsr.Close
   End If
End Sub
'Update the starred state of the given contact. Note that we use LIKE in the selection.
Sub UpdateStarred (Name As String, Starred As Boolean)
   Dim u As Uri
   u.Parse("content://com.android.contacts/contacts")
   Dim values As ContentValues
   values.Initialize
   values.PutBoolean("starred", Starred)
   Log(cr.Update(u, values, "display_name LIKE ?", Array As String("%" & Name & "%")))
End Sub

'Deletes the contact with the given name.
Sub DeleteContactByName(Name As String)
   Dim u As Uri
   u.Parse("content://com.android.contacts/data")
   Dim crsr As Cursor = cr.Query(u, Array As String("_id", "raw_contact_id"), _
      "mimetype = ? AND data1 = ?", Array As String("vnd.android.cursor.item/name", Name), "")
   For i = 0 To crsr.RowCount - 1
      crsr.Position = i
      Dim rawId As Long = crsr.GetLong("raw_contact_id")
      Log(cr.Delete(u, "raw_contact_id = ?", Array As String(rawId)))
      Dim u2 As Uri
      u2.Parse("content://com.android.contacts/raw_contacts")
      Log(cr.Delete(u2, "_id = ?", Array As String(rawId)))
   Next
   crsr.Close
End Sub

'Inserts a new contact.
Sub InsertContact(Name As String, Phone As String)
   Dim values As ContentValues
   Dim uri1 As Uri
   uri1.Parse("content://com.android.contacts/raw_contacts")
   values.Initialize
   values.PutNull("account_name")
   values.PutNull("account_type")
   Dim rawUri As Uri = cr.Insert(uri1, values)
   Dim rawContactId As Long = rawUri.ParseId
 
   uri1.Parse("content://com.android.contacts/data")
   values.Initialize
   values.PutLong("raw_contact_id", rawContactId)
   values.PutString("mimetype", "vnd.android.cursor.item/phone_v2")
   values.PutString("data1", Phone)
   cr.Insert(uri1, values)
 
   values.Initialize
   values.PutLong("raw_contact_id", rawContactId)
   values.PutString("mimetype", "vnd.android.cursor.item/name")
   values.PutString("data1", Name)
   cr.Insert(uri1, values)
End Sub

Installation instructions:
- Copy the two files in the zip to the internal libraries folder.
 
Last edited:

wineos

Member
Licensed User
Longtime User
Hi,

I want to access the CalendarContract.Instances . Can I use ContentResolver Library to read, update and delete? If yes, how can I do these?

Thanks!
 

marno

Member
Licensed User
Longtime User
ContentResolver library allows you to access "content providers" applications.
Installation instructions:
- Copy the two files in the zip to the internal libraries folder.

Where to find these .zip File?

Thanks

Marius
 

S.M.R

Member
More information please. Post the Java code.
From Android 11, LIMIT and OFFSET should be retrieved using Bundle by

Java:
public Cursor query (Uri uri,

String[] projection,

Bundle queryArgs,

CancellationSignal cancellationSignal)
 
Top