Thanks for your reply, and sorry for my late answer. :-) I tried to get it running as a service but I don't really get how I have to use services, binders and service connections. I'm reading a book with an example for services but can't adopt it to my problem. What I tried is the following:
I created one class for the service, which holds the variable for my database: __________________ public class DatabaseService extends Service { public DbAdapter mDbAdapter; private DatabaseBinder mDatabaseBinder = new DatabaseBinder(); @Override public IBinder onBind(Intent intent) { return mDatabaseBinder; } @Override public void onCreate() { super.onCreate(); mDatabaseBinder.mDatabaseService = this; mDbAdapter = new DbAdapter(getApplicationContext()); mDbAdapter.open(); } @Override public void onDestroy() { super.onDestroy(); mDatabaseBinder.mDatabaseService = null; mDbAdapter.close(); } } This is my database binder: __________________ public class DatabaseBinder extends Binder { public DatabaseService mDatabaseService; public DbAdapter getDbAdapter() { return mDatabaseService.mDbAdapter; } } And this my service connection: __________________ public class DbServiceConnection implements ServiceConnection { DatabaseBinder mBinder; public DbServiceConnection(DatabaseBinder binder) { mBinder = binder; } @Override public void onServiceConnected(ComponentName className, IBinder binder) { mBinder = (DatabaseBinder) binder; } @Override public void onServiceDisconnected(ComponentName arg0) { } } If I want to use this in my activity with this: private DatabaseBinder mDatabaseBinder; private DbServiceConnection mServiceConnection = new DbServiceConnection(mDatabaseBinder); final Intent databaseServiceIntent = new Intent(this, DatabaseService.class); this.bindService(databaseServiceIntent, mServiceConnection, Context.BIND_AUTO_CREATE); mDb = mDatabaseBinder.getDbAdapter(); I'm getting a nullpointer exception at the last line. I don't know if I'm using it right (I guess not :D ), I haven't used services before. Do you know why it is throwing a Nullpointer exception? Is this the right way to use a service and bind it in the activity or should I do it somehow different? On 19 Jul., 00:30, brucko <geoff.bruck...@gmail.com> wrote: > Bender, > > put your db in a local Service. Open the db in onCreate() close it in > onDestroy(). Your Activities can bind and unbind to the Service as > many times as you like. The system will keep the service running as > long as you have an activity in the foreground process bound to it or > otherwise until it needs to reclaim the resources. > > Take a look at : > > http://developer.android.com/resources/samples/ApiDemos/src/com/examp... > > but DONT have your binder as a non-static inner class as in the > example - or you will create a memory leak and leak your Service. > Instead, pass the binder a reference to your service in onCreate and > get the binder to null the reference out in onDestroy -- You received this message because you are subscribed to the Google Groups "Android Beginners" group. ATTENTION: Android-Beginners will be permanently disabled on August 9 2010. For more information about this change, please read [http://goo.gl/xkfl] or visit the Group home page. Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to android-beginners+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en