我编写了一个对 Poweramp 播放列表进行排序的应用程序,并且运行正常。我遇到的唯一问题是性能,我认为这是因为我在每一行上调用了 getContentResolver().update
。理想情况下,我希望能够使用 SQLite 事务。 applyBatch
和 bulkInsert
将不起作用,因为我需要在每一行更新中都有一个 WHERE 子句——我基本上必须更新一个索引表,它有多个播放列表和他们的歌。
据我所知,我将无法通过 SQLite 直接访问 Poweramp 的数据库,正如我所读,我需要具有相同的应用程序签名或其他什么?也许有人可以澄清这一点。似乎我唯一的希望是重写 ContentResolver 的 applyBatch 方法或添加我自己的方法(来自 this thread )?虽然,我不完全确定如何覆盖它……该线程中的代码似乎已经以某种方式覆盖了该类。我也不认为它使用外部数据库。
关于我应该在哪里查看的任何提示,或者如果那里已经有答案,那就太好了。
最佳答案
我最终做的是使用 ContentProviderOperation
和 getContentResolver().applyBatch
(感谢@MatPag 给出的建议,这是可能的)......现在运行快了很多,而且我不再收到 Wait 消息。 applyBatch
的关键部分是您必须将其包装在 try..catch 中,这在它报告的 Android Studio 错误中并不明显。这是最终代码:
Cursor songs = createPlaylistCursor(sort);
if (songs.moveToFirst()) {
int i = 0;
Uri playListUri = getPlaylistUri();
ArrayList<ContentProviderOperation> cs = new ArrayList<ContentProviderOperation>();
do {
cs.add(ContentProviderOperation.newUpdate(playListUri)
.withSelection("folder_file_id=" + songs.getInt(songs.getColumnIndexOrThrow("folder_files._id")), null)
.withValue("sort", i++)
.build());
} while (songs.moveToNext());
try {
getContentResolver().applyBatch("com.maxmpz.audioplayer.data", cs);
}
catch (Exception e) {
}
}
关于Android:更新另一个应用程序中使用的 SQLite 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54453563/