Lifecycle of AsyncTask and Application?
<p>My application starts with creating <code>MyApplication extends Application</code></p>

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

<p>Here is my understanding:</p>

<ol>
<li>Only one <code>Application</code> 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.</li>
<li>If I see messages I log inside <code>Application.onCreate()</code> that means that OS shut down my Application.</li>
<li>Service runs on UI thread even though not visible.</li>
<li>If I spawn <code>AsyncTask</code> 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.</li>
<li>If I had Alarm scheduled and Application was down - OS will send broadcast which will in turn start Application and then service.</li>
<li>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..</li>
</ol>

<p>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.</p>

<p>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..</p>

<pre><code>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)
</code></pre>
