AmyWuGo
AmyWuGo

Reputation: 2425

MultiThread in Android

I'm new in Android and even Java. I have to save and read message in an activity using another class "Utility". And it will cause ANR, I heard I have to do that kind of things in a separate thread.
this is my code:

Utility.save(this, message, lsn);
Message tmp = Utility.read(this, lsn);

and I tried this:

final ProgressDialog progressDialog = ProgressDialog.show(
        this, "Please wait....", "Here your message");
new Thread(new Runnable() {
    public void run() {
    Utility.save(this, message, lsn);
    Message tmp = Utility.read(this, lsn);
        progressDialog.dismiss();
    }
}).start();

and as you can imagine, there is no way go just like this. Do I need to use aidl?

Thank you guys.

Upvotes: 2

Views: 218

Answers (2)

thepoosh
thepoosh

Reputation: 12587

what you should probably do is use AsyncTask which is the Android way of multi threading in apps.

try reading in the android developers on this: http://android-developers.blogspot.co.il/2010/07/multithreading-for-performance.html

Upvotes: 4

herom
herom

Reputation: 2542

no, Service (or IntentService) or AsyncTask will do the job properly, please consider to read more about it at http://developer.android.com

Upvotes: 1

Related Questions