Calling method of applicationobject in AsyncTask - android

in my project I have several activities and 1 application defined. The application object contains some methods some of the activities are using.
Within the activity I am calling the methods of the application object out of an inner class that extends AsyncTask. Is this correct resp. is the method of the application is also executed in the AsyncTask process?
Thanks a lot!
Neo74

It depends on where you are calling that method in the Asynctask.
on "doInBackground" everything you are doing will be run on a separate thread.
and on "onPreExecute/onPostExecute" will be run on the main thread(or UI thread).
The application process along with all the threads will run as long as the system is not going to kill it.
You can read more about the process life cycle here:
http://developer.android.com/guide/components/processes-and-threads.html#Lifecycle

Related

Android AsyncTask called "multiple" times

i'm fairly new to android programming. I have a main activity that gets data from a DB through a service handler (url). I want to insert data as well, but on a different activity, and i want my main activity to be up to date each time its been called (onresume(),onrestart()).
I've found this on the Android API reference about AsyncTask:
The task can be executed only once (an exception will be thrown if a second execution is attempted.)
Does that mean that i cannot call the AsyncTask whenever the activity resumes, or that i cannot have multiple "instances" of the AsyncTask running at the same time?
It literally means, that while AsyncTask is running, you cannot launch it again.
In your MainActivity.class you have line:
task.execute();
if task either is finished or not and you call the method again then exception will be thrown.
And put this method in onResume() is a good practice.
Only one thing to notice is: if you put in onRestart() remember, this callback works when you change configuration, but it will not be called if you Create activity.
The doc about lifecycle of an Activity.
Does that mean that i cannot call the AsyncTask whenever the activity resumes, or that i cannot have multiple "instances" of the AsyncTask running at the same time?
It means you cannot call the execute method twice on the same AsyncTask instance regardless it is completed or not.

Does two activity in the same package belong to the same thread?

I have two activities under the same package (the main activity and another one). Sometimes i need to start the other activity with startActivityForResult. I have two questions:
Does the two activities belong to the same thread?
If i load a native library in the main activity, can i access the native methods from the other activity?
Regarding question 2, suppose there is a native method called NativeMethod1(). In the main activity, as usual, i have the following declaration:
public native void NativeMethod1();
and in the native code i would have a function properly declared (using Java_com_mypackage_myapp_ etc ..). I have added a similar declaration for the other activity, and, in the native library, i added a corresponding method declaration as well. All is working fine, but i fear i am assuming something that it is not always true. This would results in unpredictable crashes.
I wouldn't add native methods into your Activity class. Instead, create a singleton class that you can use from all activities.
Now as far as threading is concerned, yes the UI model for Android is single threaded (aka main thread or UI thread) so all your activities are running on the main thread.
Do not block the UI thread when you are doing long running operations as this will result in an ANR (Activity Not Responding) case, so if you are doing something fancy in your native methods you are better of using an AsyncTask or a Service or come up with your own background threading model.

Controlling a spawned Thread from multiple activities in Android

I have an application that runs a background thread which periodically performs a task. The UI thread moves through several different activities.
The tutorial I used can be found at this blog, the gist of it is the following:
Create a class that extends Thread
public final class JSONThread extends Thread {
Define a method in this class that adds a task to the MessageQueue, prompting executing when able.
public synchronized void enqueueJSON(final JSON.JSON task) {
However, after creating the initial object in my main activity, navigating to another activity obviously loses the Object bound to my Thread. I am no longer able to call methods on that Object (hence unable to add to queue).
I am unsure if this is caused by a wrong decision in architecture on my part or by overseeing the obvious solution. Any ideas? Note that I am trying to avoid AsyncTask for this purpose, since a pool of five threads for a simple task seems a little too much.
You need to store a Thread object as member of some other object with lifetime longer than Activity.
Two ideas for you:
a) It could be a member of Application (http://developer.android.com/reference/android/app/Application.html)
You may have problems with this, if you don't have a Service running. There is no guarantee that your application won't be killed (as example if any system dialog will pop up on top of your activities)
b) It could be a member of Service
(http://developer.android.com/reference/android/app/Service.html)
You should be using a service, not a thread. A service will remain in the background so long as there is an activity bound to it, and it won't be reset when an activity exits.

Why should some methods be invoked only from the main application thread?

I read about this class:
Do not instantiate this class directly, instead, call
createSpeechRecognizer(Context). This class's methods must be invoked
only from the main application thread.
I suppose that the main application thread is the main Activity of an Android application...
Why should this class's methods be invoked only from the main application thread?
The main application thread is also known as the UI thread.
This is done to ensure thread safety. (No two processes can modify the same value at the same time).
Please see: Why can only the UI thread in Android update the UI?

Async task - not clear re called methods

I have a a service class which includes an Async task. In the doInBackground and onPostExecute I call some methods which are in the service class but outside the Async task. When these methods get called will they still be in the thread created by the Async task and therefore not interfering with the main UI.
To illustrate my lack of understanding a bit more should I try to get almost everything that the service class does into the Async task. For example the service starts up as the result of an alarm and in the onStartCommand sets a repeating alarm (this is as Reto Meire's Earthquake example)and creates a database. Would it make sense to move the code for these two operations into the onPreExecute part of the Async task?
No need to do that.. make sure that the method which you want to run in background is called from doInBavkground().. rest you can put in postExecute.... the snippet which you want to run after the backGround task should be in PostExecute(). If You call methods from doInBackground() they still run on background thread.. does't matter where they are.. and if you call it from onPostExecute() then it will run on other thread which ofcourse can make changes in your display.. just like showing dialog etc...
You should always offload as much work as possible to background threads/tasks. Something like database creation should be done in the doInBackground method. The onPreExecute and onPostExecute methods run on the UI thread and are generally used to inform the user of background activity (e.g. using a ProgressDialog).
From experience, I also suggest not using a background service if you can get away with it. If you use one, you should know how to clean it up properly since users generally get annoyed with an application running in the background.

Categories

Resources