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

  • Home
  • SEARCH
  • 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 991395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:06:09+00:00 2026-05-16T06:06:09+00:00

I’ve been trying to figure out the best way to handle local database access

  • 0

I’ve been trying to figure out the best way to handle local database access in my android applications. I had been creating a database connection object in each activity but this seems like a really inefficient way to do things. Doing some research I stumbled onto this discussion. Using a Service seems like a great way to do things, but I am having trouble getting it working. Here is what I have:

SERVICE:

public class DBservice extends Service {
    private final static String TAG = "net.iamcorbin.frolfcard";
    public DBconn db;

    private DBbinder mDatabaseBinder = new DBbinder();

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG,"DBservice : onCreate");
        mDatabaseBinder.mDatabaseService = this;
        this.db = new DBconn(getApplicationContext());
        this.db.open();
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG,"DBservice : onBind");
        return mDatabaseBinder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startID) {
        Log.d(TAG,"DBservice : onStartCommand");
        return Service.START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"DBservice : onDestroy");
        mDatabaseBinder.mDatabaseService = null;
        this.db.close();
    }
}

BINDER:

public class DBbinder extends Binder {

    public DBservice mDatabaseService;

    public DBconn getDB() {
        return mDatabaseService.db;
    }
}

SERVICE CONNECTION:

public class DBserviceConn implements ServiceConnection {
    private final static String TAG = "net.iamcorbin.frolfcard";

    DBbinder mBinder;

    public DBserviceConn(DBbinder binder) {
        Log.d(TAG,"DBseviceConn : Constructor");
        this.mBinder = binder;
    }

    public void onServiceConnected(ComponentName className, IBinder binder) {
        Log.d(TAG,"DBseviceConn : OnServiceConnected");
        this.mBinder = (DBbinder) binder;
    }

    public void onServiceDisconnected(ComponentName arg0) {
        Log.d(TAG,"DBseviceConn : OnServiceDisconnected");
    }

}

ACCESSING:

private DBbinder dbBinder;
private DBserviceConn dbServiceConn;

//In onCreate() for Activity that wants to access database
//Setup DB Service Connection and Binder
this.dbServiceConn = new DBserviceConn(this.dbBinder);
final Intent i_DBservice = new Intent(PickGame.this, DBservice.class);
//bind DB Service
this.bindService(i_DBservice, this.dbServiceConn, BIND_AUTO_CREATE);

This executes without throwing any errors but when I try and use the database with:

this.dbServiceConn.mBinder.mDatabaseService.db.queryPlayers();

it throws a NullPointerException. From reading the discussion(linked above) I’m assuming that this is because the database just isn’t open yet because I’m doing the query in onCreate immediately after bindService. I need to use the database to populate a ListView though.

So the question is
1. Am I creating the service, binder, and service connection properly?
2. If so, how do I go about creating the callback to populate the ListView once the Service is started, bound, and the database opened?

  • 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-05-16T06:06:10+00:00Added an answer on May 16, 2026 at 6:06 am

    Wow, that’s a lot easier. I removed the service and just handle the database connection in the application object.

    APPLICATION:

    public class App extends Application {
        public DBconn db;
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            this.db = new DBconn(getApplicationContext());
            this.db.open();
        }
    
        @Override
        public void onTerminate() {
            this.db.close();
            super.onTerminate();
        }
    }
    

    ACCESS:

    this.app = ((App)getApplicationContext());
    
    this.lv_players_c = this.app.db.queryPlayers();
    

    Thanks Pentium10. I would still like to know if this is the most efficient way to handle the connection though. Is it fine to leave the database connection open for the duration of the Application lifecycle? Or would it be better to open and close the database whenever I need to use it in an Activity?

    Any other suggestions or confirmation of using this method would be great.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm looking for suggestions for debugging... If you view this site in Firefox or
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.