Android ContentProvider 初始化不在应用程序启动时进行
是否可以在应用程序中初始化 contentprovider?
因为我需要为每个用户/服务器组合创建一个数据库,并且我正在使用 contentprovider 的 oncreate 方法中的登录信息创建数据库!
is it possible to initialize the contentprovider in the application?
because i need to create a database for every user/server combination and I'm creating the database with the login information in the oncreate method of the the contentprovider!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据听起来您想要做的事情,您可以考虑将 ContentProvider 的数据库访问置于上下文中(这可能是描述@Gary B. 的另一种方式):
当您初始化内容提供程序时,创建一个数据库,其中您将存储用户/服务器组合的列表。然后,当用户登录应用程序时,创建相应的数据库并更新 ContentProvider 的主列表。
在 ContentProvider 的公共操作中,确定哪个用户/服务器组合处于活动状态(或正在被请求),然后在安装时创建的主数据库中查找适当的数据库。然后,您可以读取用户/服务器特定信息并将其返回给调用者。
或者,您可能可以在一个数据库中实现这一切,但我对您的要求不够了解,无法推荐一种特定的方法......
Based on what it sounds like you are trying to do, you may consider contextualizing your ContentProvider's database access (this may be another way of describing what @Gary B. was getting at):
When you initialize the content provider, create a database within which you will store the list of user/server combinations. Then, when a user logs into the application, create the corresponding database and update the master list for the ContentProvider.
In the ContentProvider's public operations, determine which user/server combination is active (or being requested) and then look up the appropriate database in the master database you created on installation. You can then read the user/server specific information and return this to the caller.
Alternatively, you could likely implement this all within one database, but I don't understand enough about your requirements to recommend a specific approach...
SQLiteOpenHelper 通常是创建数据库的地方。更具体地说
onCreate(SQLiteDatabase)
方法。安装应用程序时它将创建数据库。An SQLiteOpenHelper is usually the place to do database creation. More specifically the
onCreate(SQLiteDatabase)
method. It will create the database when the app is installed.