Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Database handling - when do you open and close
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Bender  
View profile  
 More options Aug 5 2010, 4:48 pm
From: Bender <abende...@googlemail.com>
Date: Thu, 5 Aug 2010 13:48:56 -0700 (PDT)
Local: Thurs, Aug 5 2010 4:48 pm
Subject: Re: Database handling - when do you open and close
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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.