[英]Android multiple content-providers, (DLC)
I am looking to add additional content to my app as extra databases. 我希望将额外的内容添加到我的应用程序作为额外的数据库。 Should I put them in apks and content providers, so they can be updated from google play directly, or just as new db files downloaded straight into the app. 我应该将它们放在apks和内容提供商中,这样它们可以直接从谷歌播放更新,或者直接下载到应用程序中的新数据库文件。
My problem with the content-provider/apk method, is they all have to be declared in the android manifest, and I might have multiple databases even hundreds, so would need 100s of content-provider declarations in my manifest, even when the user may have only a couple or even none of them. 我的内容提供者/ apk方法的问题是,它们都必须在android清单中声明,我可能有多个数据库甚至数百个,所以在我的清单中需要100个内容提供者声明,即使用户可能只有几个甚至没有。
Unless there is a way I can generate the manifest dynamically? 除非有办法我可以动态生成清单? Or load the content-providers outside of the manifest? 或者在清单之外加载内容提供商?
Thanks 谢谢
I think the cleanest way to do this is to have one unique ContentProvider for all your db files. 我认为最简单的方法是为所有db文件提供一个唯一的 ContentProvider 。
You should build your URI 's around database selection. 您应该围绕数据库选择构建URI 。
For example : content://com.your.package/a_db_file/something/things/5
例如: content://com.your.package/a_db_file/something/things/5
Then when implementing your ContentProvider, parse the Uri to get the a_db_file
segment, open the corresponding db file, then do the needed work according to the rest of the segments. 然后在实现ContentProvider时,解析Uri以获取a_db_file
段,打开相应的db文件,然后根据其余段执行所需的工作。
Maybe you will need a method like getCorrectDb(String a_db_file)
. 也许你需要一个像getCorrectDb(String a_db_file)
这样的方法。 Inside this method you should make the correct call to a sqlLiteOpenHelper that properly match the needed db file. 在此方法中,您应该正确调用正确匹配所需db文件的sqlLiteOpenHelper 。
Also take a look at UriMatcher , it might be useful for you. 另外,看看UriMatcher ,它可能对您有用。 :) :)
In the end you should have something like: 最后你应该有类似的东西:
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
// TODO : Parse uri to get a_db_file string.
SQLiteDatabase database = getCorrectDb(a_db_file);
// TODO : get cursor from db according to other segments of uri.
return cursor.
}
我使用一个内容提供程序来保持简单并使用“attach database”sql命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.