Android Question StartActivity(Main) Not working on Samsung Note10+

Åke Johansson Bravida

Member
Licensed User
Hello.

I have problem with my new device Samsung Note10+ I can't get the StartActivity(Main) to work. My Main activity don't start.

I have an app that I use on an old device Samsung NEO 5 and my app is working perfectly. I also tryed on an older Sony device and it's running perfectly but not on Samsung Note 10+.

Is something different in handling of this StartActivity(Main) ?
I start from my Starter file in my timer tm_tick StartActivity(Main)
 

Åke Johansson Bravida

Member
Licensed User
This is the code in Starter. The code on line 15, 78, 79, 149 and 150 is where i control my timer tm to activate my Main activty. Timer starts when the Service_Start activates.
I alway want to have my App in foreground if my App is more than 60 seconds in background it should run this code StartActivity(Main) or some other code to bring Main activity in foreground.

B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    Private working As Boolean = True
    'Private mqtt As MqttClient
    Private client As MqttClient
    Public connected As Boolean
    Public timeCount As Int = 0
    Dim mo As MqttConnectOptions
    Private listener As NotificationListener
    Dim wakelock As PhoneWakeState
    Dim tm As Timer
    Dim reconnect As Timer
    Dim beepTimer As Timer
    Private user As String = "*******"
    Private password As String = "*********"
End Sub




Sub Service_Create
    
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
    listener.Initialize("listener")
    working = True
    ConnectAndReconnect
End Sub

Sub client_Connected (Success As Boolean)
    connected = Success
    If Success Then
        ToastMessageShow("Connected!", True)
        Log("Connected to broker")
        client.Subscribe("Termoelement", 0)
        Log("Subscribe to Termoelement.")
        
    Else
        Log(LastException)
    End If
    CallSub(Main, "SetState")
End Sub

Public Sub ToggleButtonChanged(state As Boolean)
    Dim b As Byte
    If state Then b = 1 Else b = 0
    client.Publish("Termoelement", Array As Byte(b))
End Sub

Sub client_Disconnected
    connected = False
    'timeCount = 0
    CallSub(Main, "SetState")
End Sub

Sub client_MessageArrived (Topic As String, Payload() As Byte)
    Log("Message arrived. Topic=")
    Log( Topic)
    Log(" payload: ")
    Log(BytesToString(Payload, 0, Payload.Length, "UTF-8"))
    timeCount = 0
    beepTimer.Enabled=False
    'Log(Payload As Byte)


    CallSub2(Main, "setLabelText", Payload)

    
End Sub

Sub Service_Start (StartingIntent As Intent)
    wakelock.PartialLock
    listener.HandleIntent(StartingIntent)
    'ToastMessageShow("Service Initiated", True)
    tm.Initialize("tm",60000)
    tm.Enabled = True
[B]   [/B] reconnect.Initialize("reconnect", 15000)
    reconnect.Enabled=True
    beepTimer.Initialize("beepTM", 2000)
    Dim m As String = DateTime.Now + 30 * 1000
    Dim mText As StringBuilder
    mText.Initialize
    mText.Append("StartServiceAt: ")
    mText.Append (m)
    If listener.HandleIntent(StartingIntent) Then Return
    Sleep(0) 'allow the MessageReceived event to be raised.
    Service.StopAutomaticForeground
End Sub

Sub ConnectAndReconnect
    Do While working
        If client.IsInitialized Then client.Close
        client.Initialize("client", "ssl://m24.cloudmqtt.com:!!!!!!!!!", "Termoelement")
        Dim mo As MqttConnectOptions
        mo.Initialize(user, password)
        Log("Trying to connect")
        client.Connect2(mo)
        Wait For Mqtt_Connected (Success As Boolean)
        If Success Then
            Log("Mqtt connected")
            reconnect.Enabled = True
            
            Do While working And client.Connected
                client.Publish2("Termoelement", Array As Byte(0), 1, False) 'change the ping topic as needed
                Sleep(5000)
            Loop
            Log("Disconnected")
        Else
            Log("Error connecting.")
        End If
        Sleep(5000)
    Loop
End Sub

Sub Listener_NotificationPosted (SBN As StatusBarNotification)
    Log("NotificationPosted, package = " & SBN.PackageName & ", id = " & SBN.Id & _
        ", text = " & SBN.TickerText)
    Dim p As Phone
    If p.SdkVersion >= 19 Then
        Dim jno As JavaObject = SBN.Notification
        Dim extras As JavaObject = jno.GetField("extras")
        extras.RunMethod("size", Null)
        Log(extras)
        Dim title As String = extras.RunMethod("getString", Array As Object("android.title"))
        LogColor("Title = " & title, Colors.Blue)
    End If
End Sub

Sub Listener_NotificationRemoved (SBN As StatusBarNotification)
    Log("NotificationRemoved, package = " & SBN.PackageName & ", id = " & SBN.Id & _
        ", text = " & SBN.TickerText)
End Sub


Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub tm_tick
    'ToastMessageShow("tm_tick stop service!", True)
    tm.Enabled = False
    StartActivity(Main)
End Sub

Sub reconnect_tick
    Log("reconnect_tick")
    timeCount = timeCount + 1
    Log(timeCount)
    
    If timeCount > 3 Then
        CallSub(Main, "changeLabel")
        ToastMessageShow("ConnectAndReconnect !!!!", False)
        ConnectAndReconnect
    End If
    
    If timeCount > 19 Then
        reconnect.Enabled=False
        beepTimer.Enabled=True
        CallSub2(Main, "subNoData", timeCount)
        'timeCount = 0
        Else
        'ConnectAndReconnect
    End If
    reconnect.Enabled=True
    
End Sub

Sub beepTM_tick
    CallSub(Main, "BeepSound")
End Sub


Sub Service_Destroy
    
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. If you want your app to run in the background then it is safer to add another service and make that service a foreground service. Your code will work as long as you never exit foreground mode.

2. Starting from Android 10, normal apps cannot start activities while in the background. Apps that request the "draw over apps" special permission can start activities: https://www.b4x.com/android/forum/threads/draw-on-top-of-other-apps-permission.90513/
 
Upvote 0

Åke Johansson Bravida

Member
Licensed User
I try your option Nr. 2 and i have JavaObject and Phone librarys installed but i have problem with
B4X:
Dim Permission As SpecialPermission
I try to find FloatingButton library. But I can't find it?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Dim Permission As SpecialPermission
 
Upvote 0
Top