我一直在创建我的第一个应用程序的付费版本.
我想将现有数据库复制到新的付费应用程序.
这怎么可能?
编辑
在我使用的免费应用程序中SQLiteOpenHelper.他们可以使用相同的数据库,但想SQLiteOpenHelper再次使用它,并且无法弄清楚如何使用其他数据库,因为应用程序具有不同的包名称(如果他们使用相同的dos,则不会删除免费数据库时应用程序被删除?).
如果你想要更多的信息(也许你需要什么信息,请评论:))
您有几个选项,您将无法获得具有不同名称的包以直接与另一个包数据库进行交互.
因此,您可以将代码Content Provider编入免费应用程序,然后允许付费版本从内容提供商收集数据,然后在第一次运行时传递所有数据.这在我看来有点麻烦 - 但是意味着用户不需要SD卡,你也可以控制数据,IE如果用户已经使用了付费版本,你可以将数据添加到数据库而不是替换与旧的免费一个新...
您可以从免费版本中将数据库保存到SD卡,然后使用付费版本收集它.根据您想要编码的数量,您可以Broadcast Receiver在免费应用程序中设置一个,然后sendBroadcast()从付费版本设置,当免费应用程序接收广播时,它将数据库应对到SD卡,然后付费应用程序收集它.另一种方式是用户单击免费版本中的保存按钮,备份到SD卡,然后用户单击付费版本中的按钮并接收它 - 这可以像复制文件一样简单并替换应用程序的数据库,或者您可以将其作为不同的数据库导入付费应用程序,处理它将您需要的内容添加到主数据库然后丢弃它.
作为一个非常松散和简单的指针,您可以将数据库复制到SD卡中,这样就可以从代码的工作位置中删除它,因此在一定程度上应该被认为是未经测试的 - 您需要添加一些捕获在某些地方阻止它以及清单中SD卡的读写权限:
// Get hold of the db:
InputStream myInput = new FileInputStream("/data/data/com.package.name/databases/database-name");
// Set the output folder on the SDcard
File directory = new File("/sdcard/someFolderName");
// Create the folder if it doesn't exist:
if (!directory.exists())
{
directory.mkdirs();
}
// Set the output file stream up:
OutputStream myOutput = new FileOutputStream(directory.getPath()+ "/database-name.backup");
// Transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0)
{
myOutput.write(buffer, 0, length);
}
// Close and clear the streams
myOutput.flush();
myOutput.close();
myInput.close();
Run Code Online (Sandbox Code Playgroud)
Retreival非常相似:
// Set location for the db:
OutputStream myOutput = new FileOutputStream("/data/data/com.package.name/databases/database-name");
// Set the folder on the SDcard
File directory = new File("/sdcard/someFolderName");
// Set the input file stream up:
InputStream myInput = new FileInputStream(directory.getPath()+ "/database-name.backup");
// Transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0)
{
myOutput.write(buffer, 0, length);
}
// Close and clear the streams
myOutput.flush();
myOutput.close();
myInput.close();
Run Code Online (Sandbox Code Playgroud)
但是我建议先做一些检查并稍微询问用户:
就像我说上面的代码实际上只是为了给你一些关于如何使用SD卡移动数据库的提示.
| 归档时间: |
|
| 查看次数: |
3142 次 |
| 最近记录: |