简体   繁体   中英

Lifecycle of AsyncTask and Application?

My application starts with creating MyApplication extends Application

I have service that runs on background by alarm and when service done running (obtaining GPS position) I start AsyncTask from that service to do sync with my server.

Here is my understanding:

  1. Only one Application can run at a time in Android OS. I'm talking about instance of MY application. So, there is no way I can have 2 instances of my Application object from the same package running.
  2. If I see messages I log inside Application.onCreate() that means that OS shut down my Application.
  3. Service runs on UI thread even though not visible.
  4. If I spawn AsyncTask from my service and OS kills my application - AsyncTask get's terminated as well. There is no way I can control graceful termination of my AsyncTask.
  5. If I had Alarm scheduled and Application was down - OS will send broadcast which will in turn start Application and then service.
  6. Alarm starts service every 5 minutes. Service tries to obtain location for UP TO 1 minute and starts AsyncTask when done. So, my AsyncTask have 4 minutes before next service start. Inside Async task I do DB access and this causing issues like listed below..

Is my understanding correct? Reason I'm asking this because I write logs from all the devices that run application and sometimes observe weird behaviors. Most devices run Application almost 24/7 but on some I see Application_Start logged event all the time.

Is there any way to simulate Application shutdown by system for my testing? Is what I'm doing wrong and need to be done differently? I'd like to syncronize my background processing because there is a lot of data read/write and it is blocking..

java.lang.RuntimeException: An error occured while executing doInBackground()
 at android.os.AsyncTask$3.done(AsyncTask.java:299)
 at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
 at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
 at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
 at java.util.concurrent.FutureTask.run(FutureTask.java:137)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
 at java.lang.Thread.run(Thread.java:856)
Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file (code 14)
 at android.database.sqlite.SQLiteConnection.nativeExecuteForCursorWindow(Native Method)
 at android.database.sqlite.SQLiteConnection.executeForCursorWindow(SQLiteConnection.java:838)
 at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:836)
 at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
 at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:143)
 at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
 at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:196)
 at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:236)
 at com.idatt.data.LockData.Lock(LockData.java:37)
 at com.idatt.common.AsyncProcessor.doInBackground(AsyncProcessor.java:154)
 at com.idatt.common.AsyncProcessor.doInBackground(AsyncProcessor.java:18)
 at android.os.AsyncTask$2.call(AsyncTask.java:287)
 at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
 ... 4 more
android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file (code 14)
 at android.database.sqlite.SQLiteConnection.nativeExecuteForCursorWindow(Native Method)
 at android.database.sqlite.SQLiteConnection.executeForCursorWindow(SQLiteConnection.java:838)
 at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:836)
 at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
 at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:143)
 at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
 at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:196)
 at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:236)
 at com.idatt.data.LockData.Lock(LockData.java:37)
 at com.idatt.common.AsyncProcessor.doInBackground(AsyncProcessor.java:154)
 at com.idatt.common.AsyncProcessor.doInBackground(AsyncProcessor.java:18)
 at android.os.AsyncTask$2.call(AsyncTask.java:287)
 at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
 at java.util.concurrent.FutureTask.run(FutureTask.java:137)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
 at java.lang.Thread.run(Thread.java:856)
  1. wrong . android really can run multiple processes and threads (and apps) at the same time . it does it all the time , though it usually gives the foreground app more CPU than the rest . also , even though i've never done it , it's possible to run multiple instances of your own app and use multiple processes.

    however , if your app shouldn't use CPU while not in the foreground (like a game, for example) , please try to do so . nobody wants to have an app that drains the battery while it's not even seem to be running.

  2. very wrong . onCreate of the app occurs at the beginning . if it occurs multiple times, it means that it was restarted (either by the OS or by the user).

  3. correct . for background work , use a worker thread.

  4. depends . if you have the service started as a foreground service (with a notification) , the OS will give the killing of the service a very low priority .

  5. depends on what your app does . if it does it , it will occur this way .

  6. when you create a background service , you have to remember that the OS is allowed to close it whenever it thinks it doesn't do an important task . that's why you have to be able to resume your work when such a thing occurs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM