Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8494621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:21:03+00:00 2026-06-10T23:21:03+00:00

I have a difficulty with my database connection. I have a DataManager which creates

  • 0

I have a difficulty with my database connection. I have a DataManager which creates the Database connection etc. I also created an Application class which creates this DataManager so the UI Thread can get the data. Now the problem is that there is a background thread which also needs access to the database, and I am not exactly sure how to do this (currently I get an error when trying to create a new DataManager in the AsyncActivity because the database was not closed (and this is correct, as the UIThread has it open).

So I thought that (and this might not be the right way to do it as it is a different thread) I would get access to the Application and use the same DataManager on a new session. But how can I get access to the Application from an ‘ordinary’ class without Context or Activity (I have done it before I think, but cannot remember).

The code of the background task is:

public class SyncTask extends AsyncTask<String, Integer, String> {

    private static final String TAG = "Sync";

    /** application context. */
    private Context context;
    private ProgressDialog dialog;

    public SyncTask(Context aContext) {
        //this.activity = activity;
        this.context = aContext;
        dialog = new ProgressDialog(context);
    }

    protected void onPreExecute() {
        this.dialog.setMessage("Loading...");
        this.dialog.setMax(100);
        this.dialog.setProgress(0);
        this.dialog.show();
    }

    @Override
    protected void onPostExecute(final String errMessage) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }

        if (errMessage == null) {
            Toast.makeText(context, "Update completed", Toast.LENGTH_LONG).show();
        } else {
            AlertDialog alertDialog = new AlertDialog.Builder(context).create();
            alertDialog.setTitle("Error...");
            alertDialog.setMessage(errMessage);
            alertDialog.show();
        }
    }

    protected void onProgressUpdate(Integer... progress) {
        Log.d(TAG, "Progress Update: " + progress[0].toString());

        super.onProgressUpdate(progress[0]);
        dialog.setProgress(progress[0]);
    }

    protected String doInBackground(final String... args) {
        try{
            publishProgress( new Float(50).intValue());
            iDomsAndroidApp app = ((iDomsAndroidApp) ?? cannot remember ??);
            DataManager manager = app.getDataManager();
            manager.updateData();

            return null;
        } catch (Exception e){
            Log.e(TAG, "error", e);
            return e.getMessage();
        }
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:21:04+00:00Added an answer on June 10, 2026 at 11:21 pm

    You can use getApplicationContext() method of any Context instance you have and cast return to YouApplication. It is absolutelly eligible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database and a C# class library which acts as a repository
I have encounters this abap snippet that I have difficulty to comprehend. call function
I am experiencing a minor difficulty. I have a webpage, and have created a
I am having a bit of difficulty with this current code I have set
I am using Derby database with Java and Eclipse. I have a table which
I have a small (desktop) application which basically consists of an user-friendly CRUD interface
Afternoon, I am having some difficulty getting my head around this problem. I have
Let's say I have an object Employee_Storage that contains a database connection data member.
have an application in which many companies post information. the data from each company
I have a database which has a table that stores medical conditions and another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.