LogCat in Phone Library

Status
Not open for further replies.

Snant

Member
Licensed User
Longtime User
Hello to all,

I am new here so please forgive any mistakes.

I am trying to create a small example which would read the Phone's internal log and insert each line into a listview.

As i studied the Phone Library documentation, i think i could use LogCat Member with LogCatStart and LogCatStop to read any log.

Unfortunately this does not happen. Maybe i have misunderstood something ?

Here is an example:

Global Declaration
B4X:
Sub Process_Globals
   Dim Logger As LogCat
   Dim Args(1) As String
End Sub

The LogCatStart call
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Main")
   Logger.LogCatStart(Args,"LogCatData")
End Sub

The LogCatData Event
B4X:
Sub LogCatData(Buffer() As Byte, Length As Int)
   Dim data As String
   data = BytesToString(Buffer,0,Length,"UTF-8")
   ListViewLogData.AddSingleLine(data)
   Log(data)
End Sub

The LogCatStop call
B4X:
Sub ButtonStop_Click
   Logger.LogCatStop()
   ExitApplication
End Sub

The LogCatData Event is never fired. I also tried to pass some command arguments into LogCatStart according with the documentation, but nothing happened.

Thank you.
 

Snant

Member
Licensed User
Longtime User
Thank you Agraham for your answer.

Unfortunately, i changed the sub name as you mentioned but nothing changed. Maybe i should pass some specific options in Args()?

As for the Log inside the event, you are right, i've already removed it.

The weird thing is, that i get an error in the event sub, in Basic4Android's Log like: "android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.". I can't understand..

Thank you.
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
You say nothing has changed but you also say you get an error in the event! :confused:

That error is because you are accessing a UI element, probably the ListView from a thread other than the main UI thread. This is not allowed. The Logcat event is running on a separate thread to your own main thread. You could try the RunOnGuiThread method of the Thread object in my Threading library. You don't need to actually start a thread, just Dim and initialise a Thread object.

B4X:
Sub Btn1_Click
   Dim t As Thread
   t.Initialise("")
   t.RunOnGuiThread("Test", Array As Object("data"))
End Sub

Sub Test(msg As String)
   Log(msg)
End Sub
 
Upvote 0

Snant

Member
Licensed User
Longtime User
You say nothing has changed but you also say you get an error in the event! :confused:

As you can see in my previous post, there is an edit label that sais "Extra Info" which is the Exception message i discovered after my original post. :)

Your code worked. Thank you.

The documentation of this object will be fixed, as it should have been mentioned there.

I think it also should have some examples of using optional arguments because after some tests, i didn't manage to get the same results i got from adb logcat.
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
Can someone finish this example
I posted Snant code above (to save time)

thanks
 

Attachments

  • LogCat.zip
    7.1 KB · Views: 327
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    '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
End Sub

Sub Globals
    Dim Logger As LogCat
    Dim Args(1) As String
    Dim ListViewLogData As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main")
    Logger.LogCatStart(Args, "MyLogCatName")
    Dim lbl As Label = ListViewLogData.SingleLineLayout.Label
    lbl.TextColor = Colors.black
End Sub

Sub MyLogCatName_LogCatData(Buffer() As Byte, Length As Int)
    Dim data As String
    data = BytesToString(Buffer, 0, Length, "UTF-8")
    ListViewLogData.AddSingleLine(data)
End Sub

Sub ButtonStop_Click
    Logger.LogCatStop()
    ExitApplication
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

In your .bal file expand the listview to be larger
'All variants script
'AutoScaleAll 'uncomment to scale all views based on the device physical size.
ListViewLogData.SetLeftAndRight(0%x, 100%x)
ListViewLogData.SetTopAndBottom(0%y, 90%y)
ButtonStop.Bottom = 100%y
ButtonStop.Left = 0%x
and note that the line "Dim lbl …" above sets the text color to black so it is visible


and yes, it continues to update until you stop it...
Rusty
 
Upvote 0
Status
Not open for further replies.
Top