title: Creating a Service to share database connection between all activities in Android app?
body: <p>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 <a href="http://groups.google.com/group/android-beginners/browse_thread/thread/47eefb1978b9d144/b345463a5e943e9b?show_docid=b345463a5e943e9b" rel="noreferrer">this discussion</a>. Using a Service seems like a great way to do things, but I am having trouble getting it working. Here is what I have:</p>
  
  <p>SERVICE:</p>
  
  <pre><code>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();
      }
  }
  </code></pre>
  
  <p>BINDER:</p>
  
  <pre><code>public class DBbinder extends Binder {
  
      public DBservice mDatabaseService;
  
      public DBconn getDB() {
          return mDatabaseService.db;
      }
  }
  </code></pre>
  
  <p>SERVICE CONNECTION:</p>
  
  <pre><code>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");
      }
  </code></pre>
  
  <p>}</p>
  
  <p>ACCESSING:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>This executes without throwing any errors but when I try and use the database with:</p>
  
  <pre><code>this.dbServiceConn.mBinder.mDatabaseService.db.queryPlayers();
  </code></pre>
  
  <p>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.</p>
  
  <p>So the question is<br>
  1. Am I creating the service, binder, and service connection properly?<br>
  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?</p>