Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7506825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:21:36+00:00 2026-05-29T22:21:36+00:00

I am trying to make an Android music player. To make things easier, I

  • 0

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.....
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:21:39+00:00Added an answer on May 29, 2026 at 10:21 pm

    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

    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();
                }
            }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make an android app, and I am using some userbased
I am trying to make a text file on an android phone and be
I'm trying to make and android app with some dynamically drawn views. Currently, the
I am trying to make an Android interface, but I have a little overlapping
I'm trying to make a database in android but I have this problem,erase the
I am currently trying to make a game in Android. I have this piece
I'm trying to make a music-player where i use almost all the area on
I am trying to make my Android application run some code when run from
I'm trying to make some keen experiments about android event handler... Let say.., I
I'm trying to make an Android layout like the one below. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.