Android Question RingtoneManager & NB6

iz0ndg

Active Member
Licensed User
Longtime User
Hi all, I have some code to select a ringtone and then use it in a notification using NB6
B4X:
Sub btnoti_Click
  RM.ShowRingtonePicker("RM",RM.TYPE_ALARM,False,btnoti.Tag)
End Sub

Sub RM_PickerResult (Success As Boolean, Uri As String)
   Log("success..." & Success)
   Log("Uri : " & Uri)
   If Success Then
       Dim name As String
       name = ringname(Uri)
       btnoti.Tag = Uri
       lblnotiring.Text = name
'       Log("Uri :" & Uri)
'       Log("RingToneName="&name)
   Else
       Log("Pickerresult Error")
   End If
End Sub

Private Sub ringname(Uri As String) As String
   Try
       Dim R As Reflector
       'Convert the selected Uri string to a Uri Object compatible with the getRingtone method.
       Dim RTUri As Object = R.RunStaticMethod("android.net.Uri","parse",Array As Object(Uri),Array As String("java.lang.String"))
       'Get the actual ringtone object from the Uri
       R.Target = R.RunStaticMethod("android.media.RingtoneManager","getRingtone",Array As Object(R.GetContext,RTUri),Array As String("android.content.Context","android.net.Uri"))
       'Get the title from the ringtone object
       Dim Name As String = R.RunPublicmethod("getTitle",Array As Object(R.GetContext),Array As String("android.content.Context"))
       'Silent returns Unknown ringtone
       Return Name
   Catch
       Log("ERRORE NOTIFICA :" & LastException.Message)
       '           ToastMessageShow("Errore :" & LastException.Message,False)
   End Try
   Return "default"
End Sub

I save the URI as a string
now I would like to use this string for the n.CustomSound of NB6

B4X:
   Dim n As NB6
   n.Initialize("Prova Notifica", Application.LabelName, "HIGH")
   n.SmallIcon(LoadBitmapResize(File.DirAssets, "icon.png", 32dip, 32dip, True))
   'disable the default sound
   n.SetDefaults(False, True, True)
   'set custom sound
   n.CustomSound(btnoti.Tag) <---- This is a string of uri but n.CustomSound expect Fileprovider Uri object..
   Dim Notification As Notification = n.Build("Allarme " & riga1, riga2, "", Main)
   Notification.Notify(2)

can someone help me ?
Thank's.
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Notification_WithCustomSound
   Dim n As NB6
   n.Initialize("custom sound3", Application.LabelName, "DEFAULT")
   n.SmallIcon(LoadBitmapResize(File.DirAssets, "smiley.png", 32dip, 32dip, True))
   'disable the default sound
   n.SetDefaults(False, True, True)
   'set custom sound
   Dim rm As RingtoneManager 'Phone library
   Sleep(210) 'allow the list to finish the "click animation"
   rm.ShowRingtonePicker("rm", rm.TYPE_RINGTONE, False, "")
   Wait For rm_PickerResult (Success As Boolean, UriString As String)
   If Success Then
       Dim Uri As Uri 'ContentResolver library
       Uri.Parse(UriString)
       n.CustomSound(Uri)
   End If
   Dim Notification As Notification = n.Build("Notification with custom sound", "...", "", Me)
   Notification.Notify(3)
End Sub

Note that you will need to change the channel id each time that you change the sound. It can be confusing. If it doesn't work then change the id (custom sound3 in the above code).
 
Upvote 0
Top