开发者

how to do thread operations in android

开发者 https://www.devze.com 2023-03-02 01:48 出处:网络
Anyone give example program which explains android Threads in a simpler way. For example, i want to list out the songs in a sd card in thread at the time of launch

Anyone give example program which explains android Threads in a simpler way. For example, i want to list out the songs in a sd card in thread at the time of launch

i am using the following code to get the number of songs in sd card of an android device. From this i want to list out the songs in thread

private void init_phone_sd_card() 
    {   
        System.gc();
        final String[] proj = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.DISPLAY_NAME,MediaStore.Audio.Media.SIZE };
        Log.e("media",""+proj);

        musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,proj, null, null, null);
        Log.e("media1",""+musiccursor);

       开发者_Python百科 count = musiccursor.getCount();
        Log.e("media2",""+count);

how to do this......

Thx


Just put your code to AsyncTask's doInBackground() method. Task also should show ProgressDialog in order to tell user that some useful work is running. See this article.


If you are asking for a simple thread operation, this will help you..

Or you might want to use Async class like the one below,

    class AddStringTask extends AsyncTask<Void, String, Void> {
    @Override
    protected Void doInBackground(Void... unused) {
        for (String item : items) {
            publishProgress(item);
            SystemClock.sleep(1000);
        }

        return(null);
    }

    @Override
    protected void onProgressUpdate(String... item) {
        ((ArrayAdapter)getListAdapter()).add(item[0]);
    }

    @Override
    protected void onPostExecute(Void unused) {
        Toast
            .makeText(Async.this, 
                    "Done",
                    Toast.LENGTH_SHORT)
            .show();
    }
}


You need a simpler explanation. Simpler than what?

Have a look at the Android Process and Thread fundamentals and the painless threading article and its AsyncTask sections.

0

精彩评论

暂无评论...
验证码 换一张
取 消