It is possible to access your apps logs with LogCat from the Phone library.
Note that it doesn't require any permission however you cannot use it to read the logs of other apps.
The LogCatData event is raised on a different thread. This makes it a bit more difficult to work with this event if you want to process the logs.
The following code shows how to collect the logs with StringBuilder and how to use a timer to parse them:
Note that it doesn't require any permission however you cannot use it to read the logs of other apps.
The LogCatData event is raised on a different thread. This makes it a bit more difficult to work with this event if you want to process the logs.
The following code shows how to collect the logs with StringBuilder and how to use a timer to parse them:
B4X:
'Starter service
Sub Process_Globals
Private LogCat As LogCat
Public LogBuffer As StringBuilder
End Sub
Sub Service_Create
End Sub
Sub Service_Start (StartingIntent As Intent)
Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
LogBuffer.Initialize
#if RELEASE
'only start it in release mode. Debug mode doesn't support running code on background threads
LogCat.LogCatStart(Array As String(), "LogCat")
#end if
End Sub
Sub LogCat_LogCatData (Buffer() As Byte, Length As Int)
Dim b(Length) As Byte
Bit.ArrayCopy(Buffer, 0, b, 0, Length)
CallSubDelayed2(Me, "LogCatData", b)
End Sub
Sub LogCatData(buffer() As Byte)
Try
LogBuffer.Append(BytesToString(buffer, 0, buffer.Length, "utf8"))
If LogBuffer.Length > 5000 Then
LogBuffer.Remove(LogBuffer.Length - 4000, LogBuffer.Length)
End If
Catch
Log(LastException)
End Try
End Sub
'Main activity:
[code]
Sub Process_Globals
Private Timer1 As Timer
End Sub
Sub Globals
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
Timer1.Initialize("Timer1", 500)
End If
End Sub
Sub Timer1_Tick
Dim logs As String = Starter.LogBuffer.ToString
If logs.Contains("Pause") Then
Log("Good!")
Starter.LogBuffer.Remove(0, Starter.LogBuffer.Length)
End If
End Sub
Sub Activity_Resume
Timer1.Enabled = True
End Sub
Sub Activity_Pause (UserClosed As Boolean)
Timer1.Enabled = False
End Sub