android - 创建服务以在 Android 应用程序中的所有 Activity 之间共享数据库连接?

标签 android

我一直在努力找出在我的 Android 应用程序中处理本地数据库访问的最佳方式。我一直在每个 Activity 中创建一个数据库连接对象,但这似乎是一种非常低效的做事方式。做一些研究我偶然发现了this discussion .使用服务似乎是一种很好的做事方式,但我无法让它正常工作。这是我所拥有的:

服务:

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

服务连接:

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.dbServiceConn.mBinder.mDatabaseService.db.queryPlayers();

它抛出 NullPointerException。通过阅读讨论(上面链接),我假设这是因为数据库尚未打开,因为我在 bindService 之后立即在 onCreate 中进行查询。不过,我需要使用数据库来填充 ListView。

所以问题是
1. 我是否正确创建了服务、绑定(bind)器和服务连接?
2. 如果是这样,一旦服务启动、绑定(bind)并打开数据库,我该如何创建回调来填充 ListView?

最佳答案

哇,这简单多了。我删除了服务,只处理应用程序对象中的数据库连接。

应用:

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

访问:

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

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

感谢 Pentium10。我仍然想知道这是否是处理连接的最有效方法。在应用程序生命周期期间保持数据库连接打开是否可以?或者每当我需要在 Activity 中使用它时打开和关闭数据库会更好吗?

使用此方法的任何其他建议或确认都很好。

关于android - 创建服务以在 Android 应用程序中的所有 Activity 之间共享数据库连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3433883/

相关文章:

android如何在谷歌地图上画一个圆圈

android - 如何将 RelativeLayout 的宽度设置为与兄弟 View 相同?

android - 对自定义通知中的按钮单击执行操作 : Android

java - Android 与 subview 组一起旋转

java - Gradle 项目同步失败 - Android Studio 0.8.14

android - 图片和文字居中的按钮

android - 来自数据类 .copy() 的新对象

android - 包含图像的 SlidingDrawer

android - 限制在android中的webview中调用号码

android - 意外的 CoordinatorLayout 行为