Android Question How to Draw on Canvas from Service?

artemis

Member
Licensed User
Longtime User
Hello,

I have a service that's responsible for running my code and governing button actions. I'd like to be able to press a button and draw a line on the screen. I found an example how to draw something in the main activity, but is there a way to "draw" from a Service?
 
Thanks Luca, it's almost what I'm looking for but not quiet. I'm using Informatix great Overlay library and he creates his buttons inside the Service. I'd like to be able to click one of them and draw a line on the screen.
 
Last edited:
Upvote 0
Activity
Code: Copy
#Region  Project Attributes
    #ApplicationLabel: Test1
    #VersionCode: 1
    #VersionName: 1
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: Landscape
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: True
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Dim Timer_App As Timer
End Sub

Sub Globals
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
    'The timer is used to generate fake content for the list
    Timer_App.Initialize("Timer", 1500)
End Sub

Sub Activity_Resume
    'Opens the overlay window
    CallSubDelayed(SvcTest1, "ShowWindow")

    'Starts the timer
    Timer_App.Enabled = True
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    Timer_App.Enabled = False
    If UserClosed Then
        StopService(SvcTest1)
    End If
End Sub

Sub Timer_Tick
    Dim Ticks As Long = DateTime.Now   
    CallSubDelayed(SvcTest1, "ShowWindow")
End Sub

Service
Code: Copy
#Region  Service Attributes
    #StartAtBoot: False
    '#StartCommandReturnValue: android.app.Service.START_STICKY
#End Region

Sub Process_Globals
    Dim OW As OverlayWindow
    Dim OBtn_Resize, OBtn_Restore As OverlayButton
    Dim OBtn_Analyze As OverlayButton

    Dim MaxWidth, MaxHeight As Int
   
    Dim iStartWidth As Int

    
    Dim Timer_Svc As Timer
   
   
   
End Sub

Sub Service_Create
End Sub

Sub Service_Start (StartingIntent As Intent)
    'The timer is used to generate fake content for the list
    Timer_Svc.Initialize("TimerSvc", 2000)
    Timer_Svc.Enabled = True
   
   
   
End Sub

Sub Service_Destroy
    'Stops the timer
    Timer_Svc.Enabled = False

    If OW.IsInitialized Then
        'Clears the Listview
        Dim LV As ListView = OW.Panel.GetView(0)
        LV.Clear

        'Closes the window
        OW.Close
    End If
End Sub

Sub InitWindow
    'Creates the overlay window
    MaxWidth = GetDeviceLayoutValues.Width
    MaxHeight = GetDeviceLayoutValues.Height
    'TwoThirds = MaxHeight * 2 / 3
    iStartWidth = 60dip
    OW.Initialize(0, 0, iStartWidth, MaxHeight , "")
    Dim Pnl As Panel = OW.Panel
    Pnl.Color = Colors.ARGB(127, 80, 0, 0)   
   
   

    Dim bmpClose As BitmapDrawable   
    bmpClose.Initialize(LoadBitmap(File.DirAssets, "board1.png"))
    Dim mBtn As Panel
    mBtn.Initialize("mBtn")
    mBtn.Color = Colors.Yellow
    mBtn.Background= bmpClose
    Pnl.AddView(mBtn, 0, 0, -1, -1)
   
    'Creates the floating buttons
    CreateResizeBtn
    CreateAnalyzeBtn
   
End Sub

Sub CreateResizeBtn
    OBtn_Resize.Initialize(5dip, 10dip, 40dip, 40dip, "BtnResize")
    OBtn_Resize.BindTo(OW)
    Dim DrwUp, DrwDown As BitmapDrawable
    DrwUp.Initialize(LoadBitmap(File.DirAssets, "resize.png"))
    DrwDown.Initialize(LoadBitmap(File.DirAssets, "resize_pressed.png"))
    OBtn_Resize.SetStateImages(DrwUp, DrwDown, Null)
End Sub

Sub CreateRestoreBtn
    OBtn_Restore.Initialize(5dip, 10, 40dip, 40dip, "BtnRestore")
    OBtn_Restore.BindTo(OW)
    Dim DrwUp, DrwDown As BitmapDrawable
    DrwUp.Initialize(LoadBitmap(File.DirAssets, "restore.png"))
    DrwDown.Initialize(LoadBitmap(File.DirAssets, "restore_pressed.png"))
    OBtn_Restore.SetStateImages(DrwUp, DrwDown, Null)
End Sub

Sub CreateAnalyzeBtn
    OBtn_Analyze.Initialize(5dip, 50dip, 40dip, 40dip, "BtnAnalyze")
    OBtn_Analyze.BindTo(OW)
    Dim DrwUp, DrwDown As BitmapDrawable
    DrwUp.Initialize(LoadBitmap(File.DirAssets, "analyze.png"))
    DrwDown.Initialize(LoadBitmap(File.DirAssets, "analyze.png"))
    OBtn_Analyze.SetStateImages(DrwUp, DrwDown, Null)
End Sub

Sub ShowWindow
    If Not(OW.IsInitialized) Then
        InitWindow
    End If
    If Not(OW.IsOpen) Then
        OW.Open
        OW.KeepScreenOn = True
    End If
    OW.Visible = True
End Sub

Sub BtnResize_Click
    'The window occupies the whole screen
    'OW.X = 55dip
    OW.Width = GetDeviceLayoutValues.Width

    'Replaces the Resize button by the Restore button
    OBtn_Resize.UnbindFrom(OW)
    OBtn_Resize.Remove
    CreateRestoreBtn
    OBtn_Restore.Add

   
End Sub

Sub BtnRestore_Click
    'The window occupies the lower part of the screen
    'OW.Y = TwoThirds
    OW.Width = iStartWidth

    'Replaces the Restore button by the Resize button
    OBtn_Restore.UnbindFrom(OW)
    OBtn_Restore.Remove
    CreateResizeBtn
    OBtn_Resize.Add

   
End Sub

Sub BtnAnalyze_Click
    ToastMessageShow("Clicked!",False)
   
End Sub



Sub TimerSvc_Tick
    'Adds a line to the LV
    Dim Ticks As Long = DateTime.Now
   

    'Ensures that the window is visible
    ShowWindow
End Sub
 
Upvote 0
Back
Top