0

我正在构建一个警报应用程序。单击按钮后,应用程序的一部分会扫描设备的内部和外部存储器,然后显示ListView系统中的所有音频文件。相关部分代码如下:

UpdateTime.java:

public class UpdateTime extends Activity {

    Intent changeSound;
    Button alarmSound;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update_time);

        alarmSound = (Button)findViewById(R.id.alarmSound);
        alarmSound.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {

                changeSound = new Intent (UpdateTime.this, SelectAudio.class);
                startActivityForResult(changeSound, 1);         
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.update_time, menu);
        return true;
    }
}

这是找到音频文件然后将它们显示在ListActivity. 我已经在listData()其他地方检查了这个类的方法,它工作得很好,所以如果你愿意,你可以跳过阅读该部分。

SelectAudio.class:

public class SelectAudio extends ListActivity {

    public static final int selectAudioInteger = 2;

    Intent incomingIntent;
    String fileName [];
    String fileTitle [];
    String names = "";
    String titles = "";

    ContentResolver myResolver;
    Cursor myCursor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        myResolver = this.getContentResolver();
        incomingIntent = getIntent();
        listData();
        fileName = names.split("\n");
        fileTitle = titles.split("\n");

        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, fileTitle));

    }


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        String str =  fileName[position];
        incomingIntent.putExtra("fileTitle", str);
        setResult(selectAudioInteger, incomingIntent);
        Toast toast = Toast.makeText(getApplicationContext(), fileTitle[position] + " selected" , Toast.LENGTH_SHORT);
        toast.show();
        finish();
    }

    void listData()
    {
        Uri uriInternal = android.provider.MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
        Uri uriExternal = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

        Cursor curExternal = myResolver.query(uriExternal, null, MediaStore.Audio.Media.IS_MUSIC, null, null);
        Cursor curInternal = myResolver.query(uriInternal, null, MediaStore.Audio.Media.IS_MUSIC, null, null);

        int iTitleExternal = curExternal.getColumnIndex(MediaStore.Audio.Media.TITLE);
        int iTitleInternal = curInternal.getColumnIndex(MediaStore.Audio.Media.TITLE);

        int iNameExternal = curExternal.getColumnIndex(MediaStore.Audio.Media.DATA);
        int iNameInternal = curExternal.getColumnIndex(MediaStore.Audio.Media.DATA);

        while(!curExternal.moveToLast())
        {
            names = names + curExternal.getString(iNameExternal) + "\n";
            titles = titles + curExternal.getString(iTitleExternal) + "\n";
            curExternal.moveToNext();
        }

        while (!curInternal.moveToLast())
        {
            names = names + curInternal.getString(iNameInternal) + "\n";
            titles = titles + curInternal.getString(iTitleInternal) + "\n";
            curInternal.moveToLast();
        }

    }

}

这段代码的问题是,一旦单击按钮,屏幕就会变为空白,在 logcat 上,我收到一条消息,内容如下:

03-19 18:18:18.798: W/ActivityManager(59): Activity pause timeout for HistoryRecord{4a2a3d70 com.ikshvaku.intelligentalarm/.UpdateTime}
03-19 18:18:21.854: I/ActivityManager(59): Displayed activity com.ikshvaku.intelligentalarm/.SelectAudio: 3053 ms (total 3053 ms)
03-19 18:18:28.322: W/ActivityManager(59): Launch timeout has expired, giving up wake lock!
03-19 18:18:28.855: W/ActivityManager(59): Activity idle timeout for HistoryRecord{4a12ce98 com.ikshvaku.intelligentalarm/.SelectAudio}

我知道,问题的解决方案与使用 AsyncTask 类有关。我只是无法找到解决问题的方法。

任何人都可以请帮助!谢谢。

4

1 回答 1

0

当您为列表活动调用 onCreate 时...您应该放置一个加载视图并打开一个异步任务来获取您的数据...当数据最终完成加载时..然后更新列表视图...用户知道您的应用程序没有被冻结或任何东西的方式......这就是我会做的事情......假设你的其余代码完美运行!

public class SelectAudio extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        myResolver = this.getContentResolver();
        incomingIntent = getIntent();
        fileName = names.split("\n");
        fileTitle = titles.split("\n");

        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, fileTitle));
        new GetDataFromDisk().execute();
    }



    private class GetDataFromDisk extends AsyncTask<String, Void, String> {

          @Override
          protected String doInBackground(String... params) {
             listData();
             return null;
          }      

          @Override
          protected void onPostExecute(String result) {               
          }

          @Override
          protected void onPreExecute() {
            ArrayAdapter adapter = getListAdapter();
            adapter.notifyDataSetChanged();
          }
    }
}
于 2013-03-19T13:05:43.540 回答