Android Question Error : startForegroundService() not allowed due to mAllowStartForeground false

nima66

Member
I designed a feature in the application where the user can set an alarm.
The alarm is attached to an activity and selecting it will show the activity if it is not visible.
As per Erel instructions, I added the following code in the manifest:

B4X:
AddPermission(android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK)
SetServiceAttribute(alarmService, android:foregroundServiceType, "mediaPlayback")

But it still shows the following error:

B4X:
android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service idea.game.management/.alarmservice
    at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:54)
    at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:50)
    at android.os.Parcel.readParcelableInternal(Parcel.java:4870)
    at android.os.Parcel.readParcelable(Parcel.java:4852)
    at android.os.Parcel.createExceptionOrNull(Parcel.java:3052)
    at android.os.Parcel.createException(Parcel.java:3041)
    at android.os.Parcel.readException(Parcel.java:3024)
    at android.os.Parcel.readException(Parcel.java:2966)
    at android.app.IActivityManager$Stub$Proxy.startService(IActivityManager.java:6503)
    at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1942)
    at android.app.ContextImpl.startForegroundService(ContextImpl.java:1917)
    at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:841)
    at anywheresoftware.b4a.keywords.Common.StartServiceImpl(Common.java:924)
    at anywheresoftware.b4a.keywords.Common.StartService(Common.java:911)
    at idea.game.management._alarm_status_check(club.java:3033)
    at idea.game.management._load_data(club.java:10720)
    at idea.game.management$ResumableSub_Activity_Resume.resume(club.java:1711)
    at idea.game.management._activity_resume(club.java:1662)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at idea.game.management$ResumeMessage.run(club.java:317)
    at android.os.Handler.handleCallback(Handler.java:958)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:224)
    at android.os.Looper.loop(Looper.java:318)
    at android.app.ActivityThread.main(ActivityThread.java:8790)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:561)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1013)
 

nima66

Member
You cannot start a service while your app is in the background. You should use a receiver to handle the alarm intent and you cannot start the service there.
But the service is launched when the application is not in the background.
But the user may close the application after starting the service.
So what does the manifest code do?
 
Upvote 0

nima66

Member
Can you post the code in Activity_Resume?
B4X:
Sub Activity_Resume
    If func.findPf=False Then
        func.pfNotFound
        Activity.Finish
        Return
    End If

    load_data(True)
    If tm.Enabled=False Then tm.Enabled=True
    
    If editCurrentCustomer Then
        editCurrentCustomer=False
        editCurrentCustomerID=0
        If customerSelectedID>0 Then
            If factor_tag_int=0 Then
                items_value(tag_int).customerID=customerSelectedID
                items_value(tag_int).guest=""
                setCustomerName(items(tag_int).GetView(0), tag_int)
            Else
                setFactorTextTagItem(factor_tag_int, "customer", getCustomerName(customerSelectedID), customerSelectedID)
                myDB.addORupdateItem("UPDATE financial SET userID=" & customerSelectedID & ",guest='' WHERE ID=" & factor_tag_int)
            End If
            factor_tag_int=0
        End If
    End If
    If selectDevice Then
        selectDevice=False
        If deviceSelectedID>0 Then
            tag_int=deviceSelectedID-1
            popupMenu_BtnClick("Start the game")
        End If
    End If
    If selectCustomer Then
        selectCustomer=False
        If customerSelectedID>0 Then
            'If factor_tag_int=0 And customerSelectedID>0 Then
            If customerSelectedID>0 Then
                customerNameTemp=getCustomerName(customerSelectedID)
            End If
            customerRequest
        End If
    End If
    If editCurrentDevice Then
        editCurrentDevice=False
        editCurrentCustomerID=0
        If factor_tag_int=0 Then
            If deviceSelectedID>0 Then
                Private playersCount As Byte = 0
                Private itemPrice As itemPrice = findItemPrice(items_value(deviceSelectedID-1).deviceID)
                If itemPrice.p=0 Then
                    Msgbox8_show(Me, "msgbox8_result", "Select the number of players. :", Array As String("1 person", "2 person", "3 person", "4 person"))
                    Wait For (msgbox8) msgbox8_result (result As String)
                    If result="cancel" Then Return
                    playersCount=func.get_int_of_string(result)
                Else
                    playersCount=1
                End If

                items_value(deviceSelectedID-1).alarmPrice=items_value(tag_int).alarmPrice
                items_value(deviceSelectedID-1).alarmTime=items_value(tag_int).alarmTime
                items_value(deviceSelectedID-1).changePlayersCount=items_value(tag_int).changePlayersCount
                items_value(deviceSelectedID-1).comment=items_value(tag_int).comment
                items_value(deviceSelectedID-1).customerID=items_value(tag_int).customerID
                items_value(deviceSelectedID-1).factorID=items_value(tag_int).factorID
                items_value(deviceSelectedID-1).finish=items_value(tag_int).finish
                items_value(deviceSelectedID-1).guest=items_value(tag_int).guest
                items_value(deviceSelectedID-1).open=items_value(tag_int).open
                items_value(deviceSelectedID-1).pause=items_value(tag_int).pause
                items_value(deviceSelectedID-1).pauseTimes=items_value(tag_int).pauseTimes
                items_value(deviceSelectedID-1).playersCount=playersCount
                items_value(deviceSelectedID-1).price=items_value(tag_int).price
                items_value(deviceSelectedID-1).start=items_value(tag_int).start
                items_value(deviceSelectedID-1).store=items_value(tag_int).store
                items_value(deviceSelectedID-1).totalPrice=items_value(tag_int).totalPrice
                items_value(tag_int).alarmPrice=0
                items_value(tag_int).alarmTime=0
                items_value(tag_int).changePlayersCount=""
                items_value(tag_int).comment=""
                items_value(tag_int).customerID=0
                items_value(tag_int).factorID=0
                items_value(tag_int).finish=0
                items_value(tag_int).guest=""
                items_value(tag_int).open=True
                items_value(tag_int).pause=False
                items_value(tag_int).pauseTimes=""
                items_value(tag_int).playersCount=0
                items_value(tag_int).price=""
                items_value(tag_int).start=0
                items_value(tag_int).store=""
                items_value(tag_int).totalPrice=0
                setCustomerName(items(deviceSelectedID-1).GetView(0), deviceSelectedID-1)
                deviceItemVisible(tag_int, False)
                deviceItemVisible(deviceSelectedID-1, True)
            
                items_alarm(deviceSelectedID-1).Bitmap=items_alarm(tag_int).Bitmap
                items_alarm(deviceSelectedID-1).Visible=items_alarm(tag_int).Visible
                items_alarm(deviceSelectedID-1).Tag=items_alarm(tag_int).Tag
                items_alarm(tag_int).Bitmap=Null
                items_alarm(tag_int).Visible=True
                items_alarm(tag_int).Tag=0
                
                
                Private itemMainPanel As Panel
                itemMainPanel=items(deviceSelectedID-1).GetView(0)
                If items_value(deviceSelectedID-1).comment.Trim.Length>0 Then
                    itemMainPanel.Height=deviceItems_height_add
                    items(deviceSelectedID-1).Height=itemMainPanel.Height+4%x
                    items(deviceSelectedID-1).SetBackgroundImage(itemFrameLarge)
                    itemsScroll.SetHeightAt(deviceSelectedID-1, itemMainPanel.Height)
                    setComment(deviceSelectedID-1, items_value(deviceSelectedID-1).comment)
                Else
                    itemMainPanel.Height=deviceItems_height
                    items(deviceSelectedID-1).Height=itemMainPanel.Height+4%x
                    items(deviceSelectedID-1).SetBackgroundImage(itemFrameSmall)
                    itemsScroll.SetHeightAt(deviceSelectedID-1, itemMainPanel.Height)
                End If
                
                changeTimeIcon(True)
                tag_int=deviceSelectedID-1
                changeTimeIcon(True)
                tm_Tick
            End If
        Else
            If deviceSelectedID>0 Then
                btn_edit_games_Click
            End If
        End If
    End If
End Sub

How does the alarm start the activity?
If the activity is active, only the alarm is activated.
If the activity is in the background or closed, the alarm is only activated and if the user touches the notification, the activity is opened.

After displaying the notification in the alarm service, I use the following code:

B4X:
Service.StartForeground(0, notif)
 
Upvote 0

nima66

Member
Please post the full logs.

I don't understand the program flow, but it looks like the app is considered to be in the background when you are trying to start the service.
This application is responsible for managing and calculating the time of Game Club customers.
Sometimes a customer requests to be notified if the game time reaches 30 minutes.
So the user sets the time and the service starts the service at that time if it has not been started.
So practically the service never start when the application is in the background.

My Android device is 10 and since I do not encounter this error, I cannot send the full log.
But when an error occurs in the application, I save the full text of the error in the server database.

B4X:
android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service idea.game.management/.alarmservice
    at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:54)
    at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:50)
    at android.os.Parcel.readParcelableInternal(Parcel.java:4870)
    at android.os.Parcel.readParcelable(Parcel.java:4852)
    at android.os.Parcel.createExceptionOrNull(Parcel.java:3052)
    at android.os.Parcel.createException(Parcel.java:3041)
    at android.os.Parcel.readException(Parcel.java:3024)
    at android.os.Parcel.readException(Parcel.java:2966)
    at android.app.IActivityManager$Stub$Proxy.startService(IActivityManager.java:6503)
    at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1942)
    at android.app.ContextImpl.startForegroundService(ContextImpl.java:1917)
    at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:841)
    at anywheresoftware.b4a.keywords.Common.StartServiceImpl(Common.java:924)
    at anywheresoftware.b4a.keywords.Common.StartService(Common.java:911)
    at idea.game.management._alarm_status_check(club.java:3033)
    at idea.game.management._load_data(club.java:10720)
    at idea.game.management$ResumableSub_Activity_Resume.resume(club.java:1711)
    at idea.game.management._activity_resume(club.java:1662)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at idea.game.management$ResumeMessage.run(club.java:317)
    at android.os.Handler.handleCallback(Handler.java:958)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:224)
    at android.os.Looper.loop(Looper.java:318)
    at android.app.ActivityThread.main(ActivityThread.java:8790)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:561)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1013)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
So the user sets the time and the service starts the service at that time if it has not been started.
So practically the service never start when the application is in the background.
I don't understand this. Sorry.

Are you using StartServiceAt? You need to switch to receivers: Receivers and Services in 2023+
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…