Android Question Show alert Activity from Service when phone is locked

aregan

Member
Licensed User
Hi

My app needs to show to alert a user and continue to show the alert and make a sound until the user accepts or declines it.
This will need to happen even if the phone is locked or asleep.
The scenario is similar to an incoming phone call or whats app call.


I have a service that is set to Start every 30 seconds which calls a routine to determine if the user needs to be alerted.

My question is, do I need to do anything else in the service to ensure it will run every 30 seconds even if the phone is locked?
My Activity doesn't seem to show all the time when the phone is locked, are there any additional steps I need to take?
My Activity currently plays a sound on the Media volumne channel and not the phone ringer volume channel, which means the
user may have accidently turned it down. How can I play the sound through the normal ringer volumne channel ?

Has anyone got a working example of this


I have refered to these threads but still havn't managed to get it working right:


Below is an extract of the code of my current attempt.
Some issues I see testing this:
- When phone locked activity seems to load in the background and sound plays but activity not shown
- When phone locked acitivity loads in background .. then when unlocked and we see the activity it loads it again leaving the original instance of the activity loading

B4X:
'--------------------------------------------------------------------
'  Service To Check For Alerts
'--------------------------------------------------------------------
#Region  Service Attributes
    #StartAtBoot: True
    
#End Region

Sub Process_Globals
    Private lock As PhoneWakeState
End Sub

Sub Service_Create
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
    lock.PartialLock
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StartForeground(nid,  CreateNotification("...",2   )) 'I think I need this for some reason
    StartServiceAt(Me, DateTime.Now + (10 * DateTime.TicksPerSecond) , True)
    CheckForAlerts
    Service.StopAutomaticForeground 'This might help remove the icon from the top while the service is running
End Sub


Sub CreateNotification (Body As String, ImportanceLevel As Int) As Notification
    
    'ImportanceLevel
    ' Low = 2
    ' defult = 3
    'Hig = 4
    'max = 5
    
    Dim notification As Notification
    notification.Initialize2(ImportanceLevel)
    notification.Icon = "icon"
    notification.SetInfo("Test Notification", Body, "")
    Return notification
    
End Sub


Sub CheckForAlerts

    '.... Some code here that checks for Alerts and then shows the activity if required.
    
    Dim AlertMessagesCombined as string
    Dim Alert as string

    IF  Starter.lstAlerts.Size > 0 Then
      For each Alert as string in Starter.lstAlerts
            AlertMessagesCombined = AlertMessagesCombined & Alert
      Next

    StartActivity(activityAlert)
    CallSubDelayed2(activityAlert,"activityAlert", AlertMessagesCombined  )
          
    End If
End Sub




'--------------------------------------------------------------------
'  Activity to use to show and accept alerts even if phone locked
'--------------------------------------------------------------------

#Region  Activity Attributes
    #FullScreen: true
    #IncludeTitle: false
#End Region


Sub Globals
    Dim PlayID As Int
    Private lblMessage As Label
End Sub


Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("layoutAlert")
End Sub

Sub Activity_Resume

    Dim p As Phone
    Dim Line As String
 
    
    
     'Extract from https://www.b4x.com/android/forum/threads/startactivity-from-service.108771/#post-682789
    If p.SdkVersion > 28 Then
        Dim c As cls_request_drawover_persmission
        c.Initialize
        Wait For (c.GetPermission) Complete (Success As Boolean)
    End If
    
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    Starter.sounds.Stop(PlayID)
End Sub

Sub ShowAlert(Msg As String)
    lblMessage.Text = Msg
    PlayID = Starter.sounds.Play(Starter.soundid_notification,1,1,1,-1,1)
End Sub


Sub cmdCancel_Click
    Starter.sounds.Stop(PlayID)
    Activity.Finish
End Sub

Sub cmdAccept_Click
    Starter.sounds.Stop(PlayID)
    Activity.Finish
End Sub
 

Erel

Administrator
Staff member
Licensed User
I have a service that is set to Start every 30 seconds which calls a routine to determine if the user needs to be alerted.
This will not work. The minimum interval for StartServiceAt is 15 or 30 minutes.

Example of an always running app: Background location tracking
When phone locked activity seems to load in the background and sound plays but activity not shown
Show activity before lock screen: https://www.b4x.com/android/forum/threads/unlock-password-locked-screen.14870/post-84636
 
Upvote 0
Top