Android Question How to set a ringtone to a contact

tuhatinhvn

Active Member
Licensed User
Longtime User
I want to set a mp3 file to ringtone and set for a contact (exam : my girlfriend,....) how to do it with b4a?

I dont know any about java , how can i do it????
 

Ohanian

Active Member
Licensed User
Longtime User
Hi,

get this class :
B4X:
https://www.b4x.com/android/forum/threads/class-contactsutils-provides-read-write-access-to-the-stored-contacts.30824/#content

and this function to the class :
B4X:
Public Sub SetCustomRingtone (Id As Long, RingtoneURI As String)
    Dim values As ContentValues
    values.Initialize
    values.PutString("custom_ringtone", RingtoneURI)
    cr.Update(contactUri, values, "_id = ?", Array As String(Id))
End Sub

and here's the code of the main activity :
B4X:
#Region  Project Attributes 
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName: 
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Dim cu As ContactsUtils
End Sub

Sub Globals
    Dim lstContacts As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        cu.Initialize
    End If
   
    lstContacts.Initialize("lstContacts")
    Activity.AddView(lstContacts, 0, 0, 100%x, 100%y)
   
    For Each c As cuContact In cu.FindAllContacts(True)
        lstContacts.AddSingleLine2(c.DisplayName, c)
    Next
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub lstContacts_ItemLongClick (Position As Int, Value As Object)
    Dim c As cuContact = Value
   
    cu.SetCustomRingtone(c.Id, GetPathFromContentResult(File.Combine(File.DirRootExternal, "ringtone.mp3")))   
End Sub

Sub GetPathFromContentResult(UriString As String) As String
   If UriString.StartsWith("/") Then Return UriString 'If the user used a file manager to choose the image
   Dim Proj() As String
   Proj = Array As String("_data")
   Dim Cursor As Cursor
   Dim r As Reflector
   Dim Uri As Object
   Uri = r.RunStaticMethod("android.net.Uri", "parse", _
      Array As Object(UriString), _
      Array As String("java.lang.String"))
   r.Target = r.GetContext
   r.Target = r.RunMethod("getContentResolver")
   Cursor = r.RunMethod4("query", _
   Array As Object(Uri, Proj, Null, Null, Null), _
   Array As String("android.net.Uri", _
      "[Ljava.lang.String;", "java.lang.String", _
      "[Ljava.lang.String;", "java.lang.String"))

   Cursor.Position = 0
   Dim res As String
   res = Cursor.GetString("_data")
   Cursor.Close
   Return res
End Sub
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi,

get this class :
B4X:
https://www.b4x.com/android/forum/threads/class-contactsutils-provides-read-write-access-to-the-stored-contacts.30824/#content

and this function to the class :
B4X:
Public Sub SetCustomRingtone (Id As Long, RingtoneURI As String)
    Dim values As ContentValues
    values.Initialize
    values.PutString("custom_ringtone", RingtoneURI)
    cr.Update(contactUri, values, "_id = ?", Array As String(Id))
End Sub

and here's the code of the main activity :
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Dim cu As ContactsUtils
End Sub

Sub Globals
    Dim lstContacts As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        cu.Initialize
    End If

    lstContacts.Initialize("lstContacts")
    Activity.AddView(lstContacts, 0, 0, 100%x, 100%y)

    For Each c As cuContact In cu.FindAllContacts(True)
        lstContacts.AddSingleLine2(c.DisplayName, c)
    Next
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub lstContacts_ItemLongClick (Position As Int, Value As Object)
    Dim c As cuContact = Value

    cu.SetCustomRingtone(c.Id, GetPathFromContentResult(File.Combine(File.DirRootExternal, "ringtone.mp3")))
End Sub

Sub GetPathFromContentResult(UriString As String) As String
   If UriString.StartsWith("/") Then Return UriString 'If the user used a file manager to choose the image
   Dim Proj() As String
   Proj = Array As String("_data")
   Dim Cursor As Cursor
   Dim r As Reflector
   Dim Uri As Object
   Uri = r.RunStaticMethod("android.net.Uri", "parse", _
      Array As Object(UriString), _
      Array As String("java.lang.String"))
   r.Target = r.GetContext
   r.Target = r.RunMethod("getContentResolver")
   Cursor = r.RunMethod4("query", _
   Array As Object(Uri, Proj, Null, Null, Null), _
   Array As String("android.net.Uri", _
      "[Ljava.lang.String;", "java.lang.String", _
      "[Ljava.lang.String;", "java.lang.String"))

   Cursor.Position = 0
   Dim res As String
   res = Cursor.GetString("_data")
   Cursor.Close
   Return res
End Sub


For
B4X:
cr.Update(contactUri, values, "_id = ?", Array As String(Id))

Where is cr declared?

I also get a undeclared member error on
B4X:
cu.SetCustomRingtone

Looks like this coding is not complete.
 
Upvote 0

Ohanian

Active Member
Licensed User
Longtime User
For
B4X:
cr.Update(contactUri, values, "_id = ?", Array As String(Id))

Where is cr declared?

I also get a undeclared member error on
B4X:
cu.SetCustomRingtone

Looks like this coding is not complete.

Hi,

B4X:
Private cr As ContentResolver

as i said get the base class and add the function, then you have access to it.

B4X:
Public Sub SetCustomRingtone (IdAs Long, RingtoneURI AsString)Dim values AsContentValues
 values.Initialize
 values.PutString("custom_ringtone", RingtoneURI)
 cr.Update(contactUri, values, "_id = ?", ArrayAsString(Id))
End Sub
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi,

Can I get where contactUri is and also the unknown member SetCustomRingtone ?

Thanks.
 
Upvote 0
Top