我已使用内容解析器实现了AsyncTask
来从收件箱中读取短信。
我已将onPreExecute
和onPostExecute
的相应可见性代码放在ProgressBar
中,但ProgressBar
未显示。相反,我会被卡在黑色屏幕上,就像当前活动在来自之前的活动之后加载一样(即使之前活动的Toast永远停留在那里)。
public class ReadingSMS extends AsyncTask<String, String, Cursor> {
protected void onPreExecute() {
super.onPreExecute();
mLoadingIndicator.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Cursor result) {
mLoadingIndicator.setVisibility(View.INVISIBLE);
int indexDate = result.getColumnIndex("date");
int indexBody = result.getColumnIndex("body");
do {
// String str = "SMS From: " + smsInboxCursor.getString(indexAddress) +
// "\n" + smsInboxCursor.getString(indexBody) + "\n";
// arrayAdapter.add(str);
if (!stopReading) {
Date date = new Date(Long.parseLong(result.getString(indexDate)));
String newDate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(date);
//DateTime fmt = DateTime.parse(date.toString(),DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss z yyyy"));
//String formattedDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").format(date);
// Toast.makeText(this,newDate,Toast.LENGTH_SHORT).show();
Transaction tr = new Transaction(ScrollingActivity.this);
if (tr.isTransaction(result.getString(indexBody))) {
Log.d("here now bro", "yea");
tr.msg = result.getString(indexBody);
tr.date = newDate;
tr.call(ScrollingActivity.this);
}
}
} while (result.moveToNext());
}
@Override
protected Cursor doInBackground(String... params) {
return refreshSmsInbox();
}
}
}
RefreshInbox:
public Cursor refreshSmsInbox() {
ContentResolver contentResolver = getContentResolver();
Cursor smsInboxCursor = contentResolver.query(Uri.parse("content://sms/inbox"), null, null, null, null);
int indexBody = smsInboxCursor.getColumnIndex("body");
if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return null;
// arrayAdapter.clear();
return smsInboxCursor;
}
我接下来尝试的是把我的所有“工作”从onPostExecute()放到外面,之后我在execute()
上调用了AsyncTask
,然后它在几秒后显示结果状态如上所述(黑屏),但我仍然无法得到任何ProgressBar
。
任何帮助,请问这里有什么问题,如何在这种情况下实现ProgressBar
?感谢。
Cursor result = null;
try {
result = new ReadingSMS().execute().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
// I put my code from onPostExecute here.