0

我正在尝试制作一个 Android 音乐播放器。为了方便起见,我决定将手机上的艺术家复制到本地数据库,然后对本地数据进行一些自定义查询。我知道如何将 managedQuery 复制到数据库,但不能在 AsyncTask 上这样做,因为 managedQuery 只能由 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.....
4

2 回答 2

0

managedQuery已弃用。请改用 CursorLoader。

于 2012-02-16T15:24:58.173 回答
0

正如 Sparky 所说,您可以使用 CursorLoader 而不是 managedQuery。

如果您正在为 sdk 8 开发,则需要将支持包添加到您的项目中。

为避免延迟您的应用程序启动,您可以使用Service

这是一个使用服务的小例子,从 url 获取数据,然后将其插入数据库

public class GetArticlesService extends IntentService {
    private static final String TAG = "GetArticlesService";
    public GetArticlesService() {
        super("GetdArticlesService");
    }

    /* (non-Javadoc)
     * @see android.app.IntentService#onHandleIntent(android.content.Intent)
     */
    @Override
    protected void onHandleIntent(Intent intent) {
        String url = "http://example.com/artists.json";
        String response = null;



        try {
            response = Utils.httpPost(getApplicationContext(), url + "/api/GetArticles", null);

        } catch (HttpHostConnectException e) {
            e.printStackTrace();

        }

        if(!TextUtils.isEmpty(response)){
            try {
                JSONArray list = new JSONArray(response);
                if(list.length() > 0){
                    ContentValues toInsert = new ContentValues[];
                    JSONObject art = null;
                    int cant = list.length();
                    for(int i = 0;i< cant; i++){
                        toInsert.clear();
                        art = list.getJSONObject(i);
                        toInsert = new ContentValues();
                        toInsert.put(Articles._ID, art.getInt("id"));
                        toInsert.put(Articles.DESCRIPTION, art.getString("description"));
                        toInsert.put(Articles.BARCODE, art.getString("barcode"));
                        toInsert.put(Articles.RUBRO, art.getString("rubro"));
                        toInsert.put(Articles.CLAVE, art.getString("clave"));
                        getContentResolver().inert(Articles.CONTENT_URI, toInsert);
                    }

                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}
于 2012-02-16T16:18:48.657 回答