0

Androidミュージックプレーヤーを作ろうとしています。簡単にするために、電話のアーティストをローカル DB にコピーしてから、ローカル データに対していくつかのカスタム クエリを作成することにしました。managedQuery を db にコピーする方法は知っていますが、AsyncTask ではできません。managedQuery は Activity クラスからしかアクセスできないためです。アプリの起動時に Application クラスでこの呼び出しを実行しようとしています。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

Sparky が言うように、managedQuery の代わりに CursorLoader を使用できます。

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 に答える
0

managedQueryは非推奨です。代わりに CursorLoader を使用してください。

于 2012-02-16T15:24:58.173 に答える