如何从 asyncTask 使用 ManagedQuery
我正在尝试制作一个Android音乐播放器。为了让事情变得更容易,我决定将手机上的艺术家复制到本地数据库,然后对本地数据进行一些自定义查询。我知道如何将托管查询复制到数据库,但无法在 AsyncTask 上执行此操作,因为托管查询只能由 Activity 类访问。我试图在应用程序启动时在我的应用程序类中进行此调用。有谁知道在 AsyncTask 内部调用 ManagedQuery 的方法吗?我真的不想在我的第一个被调用的活动中执行此操作,因为它会显着降低我的加载速度。
这就是我想做的,尽管我知道这不会编译......
public class AplayApplication extends Application implements
OnSharedPreferenceChangeListener {
private static final String TAG = AplayApplication.class.getSimpleName();
private SharedPreferences prefs;
protected MusicData musicData;
protected PlayerHandler mMediaPlayer;
protected boolean isPlaying;
private boolean prefUseDefaultShuffle;
private boolean prefUseSmartShuffle;
private int prefArtistSkipDuration;
private int prefUnheardArtistPct;
protected TabHost tabHost;
protected Song currentSong;
protected int currentSongPosition;
private static final String PREFERENCE_KEY = "seekBarPreference";
protected boolean hasLoadedSongs;
private static AplayApplication aplayapp;
@Override
public void onCreate() {
super.onCreate();
prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
setPrefs();
Log.i(TAG, "Application started");
mMediaPlayer = new PlayerHandler();
// code in question below this line
musicData = new MusicData(this); // this creates instance of database helper to access db
// will call execute on async task here.
// new getArtist().execute();
}
private class getArtists extends AsyncTask<Void, Void, Boolean>{
Cursor artCursor;
@Override
protected Boolean doInBackground(Void... params) {
String[] proj = {
MediaStore.Audio.Artists._ID,MediaStore.Audio.Artists.ARTIST,
};
artCursor = managedQuery(
MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, proj, null,
null, MediaStore.Audio.Artists.ARTIST + " ASC");
ContentValues values = new ContentValues();
artCursor.moveToPosition(-1);
while (artCursor.moveToNext()) {
values.put(
MusicData.S_DISPLAY,
newMusicCursor.getString(newMusicCursor
.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));
values.put(MusicData.S_ARTIST, newMusicCursor
.getString(newMusicCursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST)));
values.put(MusicData.S_FILE, newMusicCursor
.getString(newMusicCursor
.getColumnIndex(MediaStore.Audio.Media.DATA)));
this.musicData.insertMastSong(values);
}
return true;
}
//// code continues.....
I am trying to make an Android music player. To make things easier, I have decided to copy the Artists on the phone to a local DB and then make some custom queries to the local data. I know how to copy the managedQuery to a db, but cannot do so on an AsyncTask since managedQuery is only accessible by an Activity class. I am trying to do this call in my Application class upon app startup. Does anyone know a way to call managedQuery inside of the AsyncTask? I really do not want to do this in my first activity that is called since it will slow my load speed significantly.
This is what I would like to do, although I know this will not compile...
public class AplayApplication extends Application implements
OnSharedPreferenceChangeListener {
private static final String TAG = AplayApplication.class.getSimpleName();
private SharedPreferences prefs;
protected MusicData musicData;
protected PlayerHandler mMediaPlayer;
protected boolean isPlaying;
private boolean prefUseDefaultShuffle;
private boolean prefUseSmartShuffle;
private int prefArtistSkipDuration;
private int prefUnheardArtistPct;
protected TabHost tabHost;
protected Song currentSong;
protected int currentSongPosition;
private static final String PREFERENCE_KEY = "seekBarPreference";
protected boolean hasLoadedSongs;
private static AplayApplication aplayapp;
@Override
public void onCreate() {
super.onCreate();
prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
setPrefs();
Log.i(TAG, "Application started");
mMediaPlayer = new PlayerHandler();
// code in question below this line
musicData = new MusicData(this); // this creates instance of database helper to access db
// will call execute on async task here.
// new getArtist().execute();
}
private class getArtists extends AsyncTask<Void, Void, Boolean>{
Cursor artCursor;
@Override
protected Boolean doInBackground(Void... params) {
String[] proj = {
MediaStore.Audio.Artists._ID,MediaStore.Audio.Artists.ARTIST,
};
artCursor = managedQuery(
MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, proj, null,
null, MediaStore.Audio.Artists.ARTIST + " ASC");
ContentValues values = new ContentValues();
artCursor.moveToPosition(-1);
while (artCursor.moveToNext()) {
values.put(
MusicData.S_DISPLAY,
newMusicCursor.getString(newMusicCursor
.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));
values.put(MusicData.S_ARTIST, newMusicCursor
.getString(newMusicCursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST)));
values.put(MusicData.S_FILE, newMusicCursor
.getString(newMusicCursor
.getColumnIndex(MediaStore.Audio.Media.DATA)));
this.musicData.insertMastSong(values);
}
return true;
}
//// code continues.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Sparky 所说,您可以使用 CursorLoader 而不是 ManagedQuery。
如果您正在开发 sdk 8,则需要将 支持包 添加到您的项目中。
为了避免延迟应用程序启动,您可以使用 Service。
这是一个使用服务的小例子,从 url 获取数据,然后将其插入数据库
As Sparky says, you could use CursorLoader instead of managedQuery.
If you are developing for sdk 8 you need to add Support Package to your project.
To avoid delay your application start maybe you could use a Service.
This is a little example for use a service, get the data from an url and then insert it to the database
managedQuery 已弃用。请改用 CursorLoader。
managedQuery is deprecated. Use CursorLoader instead.