Android Question Unwanted Notification

MrKim

Well-Known Member
Licensed User
Longtime User
I made some minor changes to one of my apps and found that the Widget was leaving an unwanted and unremoveable icon on the notification bar. Found the appropriate thread:
https://www.b4x.com/android/forum/threads/automatic-foreground-mode.90546/
And added:
B4X:
Sleep(0)
Service.StopAutomaticForeground
as the last thing in Service_Start to all service modules. This resolved THAT issue however, my widget contains several Labels which open the main program. I found that opening the program put my icon on the notification bar - it would disappear when I closed the app but there was no reason for it to be there.

I have resolved the issue by adding the above code to each of the labels in my widget, ie:
B4X:
Sub Label1_Click
    StartActivity(Main)
    Sleep(0)
    Service.StopAutomaticForeground
End Sub

However as I read it this shouldn't be necessary?
Am I missing something?

android:targetSdkVersion="26"
 

MrKim

Well-Known Member
Licensed User
Longtime User
Can you post the Service_Start code?
I am assuming you are talking about the widgets service.
Here is the whole service:
B4X:
#Region  Service Attributes
    #StartAtBoot: False
    
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim RV As RemoteViews
End Sub

Sub Service_Create
Try
     RV = ConfigureHomeWidget("swlwidget", "rv", 2000, "SwipeList",  False)'unrem for release
Catch
'    Log(LastException.Message)
    ToastMessageShow(LastException.Message, True)
End Try

End Sub

Sub Service_Start (StartingIntent As Intent)
    
    If RV.HandleWidgetEvents(StartingIntent) Then Return'unrem for release
    Sleep(0)
    Service.StopAutomaticForeground

End Sub

Sub Service_Destroy

End Sub

Sub RV_RequestUpdate
        RestoreWidgetData
End Sub

Sub RV_Disabled
    StopService("")
End Sub

Sub SwLPnl_Click
    StartActivity(Main)
    Sleep(0)
    Service.StopAutomaticForeground
End Sub
Sub Label1_Click
    StartActivity(Main)
    Sleep(0)
    Service.StopAutomaticForeground
End Sub
Sub Label2_Click
    StartActivity(Main)
    Sleep(0)
    Service.StopAutomaticForeground
End Sub
Sub Label3_Click
    StartActivity(Main)
    Sleep(0)
    Service.StopAutomaticForeground
End Sub
Sub Label4_Click
    StartActivity(Main)
    Sleep(0)
    Service.StopAutomaticForeground
End Sub
Sub Label5_Click
    StartActivity(Main)
    Sleep(0)
    Service.StopAutomaticForeground
End Sub
Sub Label6_Click
    StartActivity(Main)
    Sleep(0)
    Service.StopAutomaticForeground
End Sub
Sub Panel1_Click
    StartActivity(Main)
    Sleep(0)
    Service.StopAutomaticForeground
End Sub

Public Sub RestoreWidgetData
    Dim R As RandomAccessFile, It As List, SS As NotePoperties, i2 As Int
    Try
        R.Initialize(File.DirInternal, "SwipeList.swl", False)
        It = R.ReadB4XObject(0)
        R.Close
        'ToastMessageShow("Refreshing widget data", False)
        i2 = It.Size
        'ToastMessageShow("Size " & i2, False)
        Dim CV As Canvas, BMP As Bitmap
        BMP.InitializeMutable(155dip, 50dip)
        CV.Initialize2(BMP)
        If i2 > 0 Then
'            Log(1)
            SS = It.Get(0)
            RV.SetText("Label1", SS.Txt)
            RV.SetVisible("Label1", True)
            RV.SetVisible("IV1", True)
            RV.SetTextColor("Label1", SS.TxtClr)
            CV.DrawColor(SS.BClr)
            RV.SetImage("IV1", BMP)
        Else
            RV.SetText("Label1", "List is empty. Top 6 in your list will display here.")
            RV.SetVisible("Label1", True)
            RV.SetTextColor("Label1", Colors.White)
            CV.DrawColor(Colors.Blue)
            RV.SetImage("IV1", BMP)
        End If
        If i2 > 1 Then
'            Log(2)
            'Dim CV As Canvas
            'CV.Initialize2(BMP)
            SS = It.Get(1)
            RV.SetText("Label2", SS.Txt)
            RV.SetVisible("Label2", True)
            RV.SetVisible("IV2", True)
            RV.SetTextColor("Label2", SS.TxtClr)
            Dim CV As Canvas, BMP As Bitmap
            BMP.InitializeMutable(155dip, 50dip)
            CV.Initialize2(BMP)
            CV.DrawColor(SS.BClr)
            RV.SetImage("IV2", BMP)
        Else
            RV.SetVisible("Label2", False)
            RV.SetVisible("IV2", False)
        End If
        If i2 > 2 Then
            SS = It.Get(2)
            RV.SetText("Label3", SS.Txt)
            RV.SetVisible("Label3", True)
            RV.SetVisible("IV3", True)
            RV.SetTextColor("Label3", SS.TxtClr)
            Dim CV As Canvas, BMP As Bitmap
            BMP.InitializeMutable(155dip, 50dip)
            CV.Initialize2(BMP)
            CV.DrawColor(SS.BClr)
            RV.SetImage("IV3", BMP)
        Else
            RV.SetVisible("Label3", False)
            RV.SetVisible("IV3", False)
        End If
        If i2 > 3 Then
            SS = It.Get(3)
            RV.SetText("Label4", SS.Txt)
            RV.SetVisible("Label4", True)
            RV.SetVisible("IV4", True)
            RV.SetTextColor("Label5", SS.TxtClr)
            Dim CV As Canvas, BMP As Bitmap
            BMP.InitializeMutable(155dip, 50dip)
            CV.Initialize2(BMP)
            CV.DrawColor(SS.BClr)
            RV.SetImage("IV4", BMP)
        Else
            RV.SetVisible("Label4", False)
            RV.SetVisible("IV4", False)
        End If
        If i2 > 4 Then
            SS = It.Get(4)
            RV.SetText("Label5", SS.Txt)
            RV.SetVisible("Label5", True)
            RV.SetVisible("IV5", True)
            RV.SetTextColor("Label5", SS.TxtClr)
            Dim CV As Canvas, BMP As Bitmap
            BMP.InitializeMutable(155dip, 50dip)
            CV.Initialize2(BMP)
            CV.DrawColor(SS.BClr)
            RV.SetImage("IV5", BMP)
        Else
            RV.SetVisible("Label5", False)
            RV.SetVisible("IV5", False)
        End If
        If i2 > 5 Then
            SS = It.Get(5)
            RV.SetText("Label6", SS.Txt)
            RV.SetVisible("Label6", True)
            RV.SetVisible("IV6", True)
            RV.SetTextColor("Label6", SS.TxtClr)
            Dim CV As Canvas, BMP As Bitmap
            BMP.InitializeMutable(155dip, 50dip)
            CV.Initialize2(BMP)
            CV.DrawColor(SS.BClr)
            RV.SetImage("IV6", BMP)
        Else
            RV.SetVisible("Label6", False)
            RV.SetVisible("IV6", False)
        End If
'        Log("Update")
        RV.UpdateWidget
        Sleep(0)
        Service.StopAutomaticForeground
    Catch
        ToastMessageShow(LastException.Message, True)
    End Try

End Sub

You can see where I added ....StopAutomaticForeground to each of the Click events to prevent the spurious notification.
 
Upvote 0
Top