创建服务来分享Android应用程序的所有活动之间的数据库连接? [英] Creating a Service to share database connection between all activities in Android app?

查看:85
本文介绍了创建服务来分享Android应用程序的所有活动之间的数据库连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图找出处理在我的Andr​​oid应用程序的本地数据库访问的最佳方式。我一直在创造中的每个活动的数据库连接对象,但是这似乎是一个非常低效的方式来做事。做一些研究,我无意中发现了<一href="http://groups.google.com/group/android-beginners/browse_thread/thread/47eefb1978b9d144/b345463a5e943e9b?show_docid=b345463a5e943e9b">this讨论。使用服务似乎是一个伟大的方式做事情,但我无法得到它的工作。以下是我有:

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:

服务:

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();
    }
}

粘结剂:

public class DBbinder extends Binder {

    public DBservice mDatabaseService;

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

服务连接:

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");
    }

}

访问:

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();

它抛出一个NullPointerException异常。从阅读的讨论(上面链接)我假设,这是因为数据库只是尚未打开,因为我马上bindService后在做的onCreate查询。我需要使用数据库来虽然填充一个ListView。

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.

所以,问题是
1.我是创建服务,粘合剂和服务的连接是否正确?
2.如果是这样,我怎么去创造回调填充ListView服务一旦被启动,装订成册,数据库打开?

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?

推荐答案

哇,这是一个容易得多。我删除了服务,只是处理应用程序中的对象的数据库连接。

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

应用:

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:

ACCESS:

this.app = ((App)getApplicationContext());

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

感谢Pentium10。我还是想知道这是否是最有效的方法,虽然处理连接。它是精细,使数据库连接打开的应用程序生命周期的持续时间?或者会是最好打开和关闭数据库,每当我需要使用它在一个活动?

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.

这篇关于创建服务来分享Android应用程序的所有活动之间的数据库连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆