AsyncTask and SQLite Database

What's the best way to access the database through an async task?

I don't think I should pass in a reference to the DbAdapter that the activity is using (could be closed as the activity may be garbage-collected).

Also, the db needs a context to be opened and closed, but I don't have that with the async task.


Asked by: Oliver432 | Posted: 24-01-2022






Answer 1

AsyncTask is an abstract class, so you have to extend it.

Create a class which extends AsyncTask, then in the constructor of this class you can add every things you want like the Context.

Answered by: Caroline425 | Posted: 25-02-2022



Similar questions

Whether I should use Asynctask or a service to update Database in Android

My requirement is to get data from a webpage and update in the Android Database (A simple table) and from there I will be querying the database to send out notifications Whether I should use Async task or Service for it?


android - Using AsyncTask with database

I have the following code: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.screen_database); db=new DBAdapter(this); InitTask init_task=new InitTask(); init_task.execute(db); } public class InitTask extends AsyncTask <DBAdapter, Void, List<GeoPoint>> { List<GeoPoint> geoPointsArray = new Array...


android - Database access from AsyncTask and Activity - locks. Solution?

I have android app that talks to server and syncs some data int SQLite database. There is service that runs every 5 minutes and spans AsyncTask that grabs data and insert/update data into database. Here is my issue. When background process updates database it might take some considerable time. If user trying to use application at the same time and opens ListView with cursor bound to that same table - I get deadlock...


android - Database blocked and AsyncTask

Is there a possibilty to make a thread that is only for showing a progressdialog while a method is running, without putting the operation itself inside the thread, as it is done in AsyncTask? Let’s say first line of method is something like startThread with something like ProgDialog.show(), last line is something like stopThread with something like Pr...


android - Insert data into database using AsyncTask

Android 2.3.3 I am new to using AsyncTask, so, my implementation may or may not be right. I have to insert data into database using a AsyncTask. The problem is there are no errors / exceptions, but the database is not being created. I didn't know whether the problem is related to database or asynctask. So I am providing the code below. please have a look. Database File ::: import an...


Android AsyncTask locks Database

I've got Fragments, where I cache data in every Fragment. I cache the data in an AsyncTask by downloading it in the doInBackground method from a server and save it in the onPostExecute to my database. So I always open the database connection in the onPostExecution. If I scroll "fast" threw the fragments, I think the AsyncTasks are pass the previews instances and there will be a android.database.sqlite.SQLiteOpenHelpe...


android - SQLite database running AsyncTask, how to use

I have a fundamental confusion about SQLite. Do I make a database instance in the main? Does SQLite run on a background thread or do I have to put it in an AsyncTask? (If so, should it be the same asynctask as the one retrieving information?) I am parsing an HTTP POST in an AsyncTask in my MainActivity(I get a list of vehicles whose information needs to be monitored), and I would like to put this ArrayList into an ...


android - Using AsyncTask to read items from a database

I decided to add an AsyncTask to a fragment in my app in order to receive items from the database more efficiently. I set my generics to Void, Void, List<Object> (where Object is an object I created), and the inner methods as follows: private class GetObjectsTask extends AsyncTask<Void, Void, List<Object>> { @Override protected List&...


database - Android How to call methods from my DAO through Service using AsyncTask?

In my app, I use Service and need to call my DAO methods for CRUD operations.. In my DAO there are several methods.. These methods must be called an AsyncTask, but my question is how to call the methods of my DAO's without having to create an AsyncTask for each method of the my DAO? E.g: i need call DAO methods in AsyncTask in this sequence: MyService -> MyDAO.getAll() MyService -> MyDAO.inser...


android - SqLite Database error inside asynctask

I run some DB stuff inside an asynctask that runs inside a runnable via handler.postdelayed(r, 30000). It gets repeated (as you can see, every 30 seconds). Sometimes, not everytime, my app is crashing. Below is the logcat. 09-24 18:28:52.813: W/dalvikvm(6194): threadid=12: thread exiting with uncaught exception (group=0x40daf1f8) 09-24 18:28:52.823: E/AndroidRuntime(6194): FATAL EXCEPTION: AsyncTas...


android - What to do with AsyncTask in onPause()?

I'm using an AsyncTask in my activity. Looks like: public class MyActivity { private AsyncTask mTask; private void doSomethingCool() { mTask = new AsyncTask(...); mTask.execute(); } @Override protected void onPause() { super.onPause(); // How do I let the task keep running if just rotating? if (isFinishing() == false) { ... ...


android - How to get XML using AsyncTask and Timer?

In order to get XML data from a server repeatedly, I'm attempting to use AsyncTask and Timer as per Mark Murphy's suggestion. I get the following error: 01-07 16:11:26.705: ERROR/AndroidRuntime(729): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has ...


java - Problem with AsyncTask on rooted Droid

I've got a widget on the Android market called DigiClock widget, and after the last update i've been having some extremely rare and random problems on rooted Motorola Droids ( there may be other handsets with the problem, but the only responses i've had are from rooted droid users ). The problem occurs when an activity is launched that runs an AsyncTask that retrieves all the installed applications from the device while sh...


junit - Android AsyncTask testing with Android Test Framework

I have a very simple AsyncTask implementation example and am having problem in testing it using Android JUnit framework. It works just fine when I instantiate and execute it in normal application. However when it's executed from any of Android Testing framework classes (i.e. AndroidTestCase, ActivityUnitTestCase, ActivityInstrumentationTestCase2...


android - AsyncTask Bug on HTC Sense

Im using HTC Hero with HTS sense. Im experience that sometimes AsyncTask not will run doInBackground method on execute(); Its only on my Hero this appears. Someone have come across same problem? / Martin


java - I have one Activity. In this Activity, I executed a AsyncTask , but it's not working

private class ExecuteLocations extends AsyncTask<String, Void, Void>{ private final ProgressDialog dialog = new ProgressDialog(ListProfiles.this); protected void onPreExecute() { //this.dialog.setMessage("Starting pre-execute..."); //this.dialog.show(); } @Override protected Void doInBackground(String... arg0) { check_profiles_lm=(LocationManager) ListProfiles.thi...


java - When to use a Service or AsyncTask or Handler?

Can someone tell me the TRUE difference?


android - How to cancel AsyncTask when Activity finishes?

In my Activity I use multiple AsyncTask classes. How to cancel AsyncTask when Activity finishes?


android - AsyncTask won't stop even when the Activity has destroyed

I have an AsyncTask object which starts executing when the Activity is created and does stuff in the background (downloads up to 100 images). Everything works fine but there is this peculiar behavior which i'm not able to understand. For eg: when the android screen's orientation changes then the Activity is destroyed and created again. So I override the o...


android - AsyncTask Threading Rule - Can it really only be used once?

In the documentation on AsyncTask it gives the following as a rule related to threading: The task can be executed only once (an exception will be thrown if a second execution is attempted.) All this means is that you have to create a new instance of the class every ti...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top