Android Question Background Tasking / Threading

DennisW

Member
Licensed User
Longtime User
Hey guys,

I have written an App which works with contacts in Basic4Android.

My App downloads Facebook Friends Information and saves it to the phone, but to save more than 300 contacts to my devices contacts takes some time and if I do it on MainThread, it hangs my whole Phone for about one Minute completely.

I will need to do this operation in Background, but I cant find a way to do it with B4A.

In Java I would use AsyncTask<>... But I have written my App with B4A now and I wouldnt like to write it completely new in Java.... :)

Is there a way to do a background job in B4A?

Thank you for your help in advance

regards

Dennis
 

DennisW

Member
Licensed User
Longtime User
Hi,

thank you, but this Libary is completly bugy... :)

I have build up a background Task like descriped in the example in this zip, but it only runs sometimes...

Mostly there are different Errors, with the same code....

Often its this one: "Java.Lang.runtimeexception: can't create handler inside thread that has not called looper.prepare()"

Sometimes it is: "Exception: Only the original thread that created a view hierarchy can touch its views."

Im using the following Code.... it also has worked a couple times... but than it crashes again and again...

Is there someone who can fix this? :)

B4X:
Dim myThread As Thread
Dim Lock1 As Lock
....
Sub Activity_Create()
If myThread.IsInitialized =False Then
myThread.Initialise("myThread")
End If
End Sub
...

myThread.Name = "BackThread1"
Dim args(0) As Object
Lock1.Initialize(True)
myThread.Start(Null, "SavePhone", args)
 
Upvote 0

socialnetis

Active Member
Licensed User
Longtime User
Sometimes it is: "Exception: Only the original thread that created a view hierarchy can touch its views."
Also de documentation says:
Such threads are not allowed to manipulate GUI elements directly but this can be
achieved by the Thread.RunOnGuiThread method that runs a Sub on the main GUI thread.
 
Upvote 0

DennisW

Member
Licensed User
Longtime User
Hi,

Im realy sorry for wasting your time, its like you said, never ever make a GUI manipulation from a background thread...

Since 4 years now Im writing real big apps in objective C for iOS devices handeling the same procedure: you MUST NOT interact with any View from Background tasks, and on my first steps in Basic 4 Android Im acting like a rookie himself :)

Thank you for your help anyway, you realy made my day! :)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You most probably do not need the Threading library (though you can use it).

The downloading step is always done in the background.
Are you using ContactsUtils to add the contacts?
ContactsUtils is based on ContentResolver library. This library also includes 'async' methods (QueryAsync and InsertAsync). You can change the code to use the async methods instead.

As a general concept, libraries should allow the developer to run "slow" tasks in the background without requiring the developer to directly manage secondary threads. There are some exceptions like OpenGL or real time games.
 
Upvote 0
Top