我正在构建一个警报应用程序。单击一个按钮后,应用程序的一部分将扫描设备的内部和外部存储器,然后显示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类有关。我只是无法找到如何解决问题。
任何人都可以帮忙!! 感谢。
答案 0 :(得分: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();
}
}
}