Android Question Microphone loses access/permission in the Background

abbas abedi

Member
this is app code :
B4X:
#Region  Service Attributes
    #StartAtBoot: False
 
#End Region

Sub Process_Globals
    Dim audioManager As JavaObject
    Public recoder As AudioStreamer
    Dim listo As List
End Sub

Sub Service_Create
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StartForeground(1, CreateNotification("..."))

    recoder.Initialize("recoder",22050,True,16,recoder.VOLUME_MUSIC)
    recoder.StartRecording

    listo.Initialize
    listo.Clear
 
End Sub

Sub Service_Destroy
    Dim r As Reflector
    r.Target = recoder
    r.Target = r.GetField("audioRecord")
    r.RunMethod("release")
 
    StartServiceAt(Me, DateTime.Now + 20 * DateTime.TicksPerSecond, True)
End Sub

Sub CreateNotification (Body As String) As Notification
    Dim notification As Notification
    notification.Initialize2(notification.IMPORTANCE_LOW)
    notification.Icon = "icon"
    notification.SetInfo("Calling", Body, Main)
    Return notification
End Sub

Sub call_wait(c As Boolean)
    If c = True Then
        recoder.StopRecording
    Else
        recoder.StartRecording
    End If
End Sub

Sub audio_Event (MethodName As String, Args() As Object) As Object
    Try

        If  Args(0).As(Int)=-2 Then
            call_wait(True)

        Else if Args(0).As(Int)=-1 Then
            call_wait(True)
     
        else if Args(0).As(Int)=1 Then
           call_wait(False)
        End If
    Catch
    End Try
 
    Return Null
End Sub

Sub recoder_RecordBuffer (Data() As Byte)
        Private b As B4XSerializator
        If listo.IsInitialized Then listo.Add(b.ConvertObjectToBytes(Data))
End Sub

B4X:
AddPermission(android.permission.RECORD_AUDIO)
AddPermission(android.permission.RECEIVE_BOOT_COMPLETED)
AddReceiverText(MyReceiver,
<intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>)

the phone's microphone not work after removed from recent app and wake service Foreground.
The purpose of this program is to record the sound of the environment after the program is completely closed.
android SDK is 34
 
Last edited:
Upvote 0

abbas abedi

Member
Are you sure that your code is running?

Your code will not work on Android 14+ devices.
When the app is in use, this code works. However, when the app is closed, the service is called in the background, but the phone's microphone is not accessible. Is there any way to solve this issue in Android 14, such as using device administrator or any other permission?
This is a challenge for me!
 
Upvote 0
Top