您好我正在开发一个Android应用程序,我试图从内置的短信应用程序加载短信。此加载速度太慢,加载所有短信几乎需要10-15秒。我正在接受短信 doInBackground如下
private class MyBackgroundTask extends AsyncTask<Context, Integer, Boolean>
{
@Override
protected void onPreExecute()
{
pDialog = new ProgressDialog(Myapp.this);
pDialog.setMessage("Loading ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Boolean doInBackground(Context... params)
{
//fetching values from built-in message
getSMS();
return true;
}
@Override
public void onPostExecute(Boolean success)
{
pDialog.dismiss();
runOnUiThread(new Runnable()
{
public void run()
{
adapter = new SmsAdapter(EaseSms.this, listOfmessages);
smslist.setAdapter(adapter);
}
});
}
}
和getSMS()方法如下
public void getSMS()
{
Uri uriSMSURI = Uri.parse("content://mms-sms/conversations?simple=true");
Cursor cursor = getContentResolver().query(uriSMSURI, new String[] {"*"}, null, null, "date desc");
while (cursor.moveToNext())
{
snippet = cursor.getString(cursor.getColumnIndexOrThrow("snippet"));
recipient_ids = cursor.getString(cursor.getColumnIndexOrThrow("recipient_ids"));
Cursor c = getContentResolver().query(Uri.parse("content://mms-sms/canonical-addresses"), null, "_id = " + recipient_ids, null, null);
c.moveToFirst();
r_address = c.getString(c.getColumnIndexOrThrow("address"));
Uri Nameuri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(r_address));
Cursor cs= getContentResolver().query(Nameuri, new String[]{PhoneLookup.DISPLAY_NAME,PhoneLookup._ID},PhoneLookup.NUMBER+"='"+r_address+"'",null,null);
if(cs.getCount()>0)
{
while (cs.moveToNext())
{
contactName = cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME));
contactID = cs.getString(cs.getColumnIndex(PhoneLookup._ID));
}
}
else
{
contactID = "01234567890";
contactName = r_address;
}
cs.close();
c.close();
addtomap();
}
cursor.close();}
}
是否可以加载10个短信onPreExecute并继续在doInBackground中加载其他内容?我尝试了类似的东西。但它有效。任何人都可以指导我解决这种缓慢加载问题。
请帮助。
谢谢!
答案 0 :(得分:0)
尝试使用光标适配器,而不是先尝试读取所有短信然后再显示它们。光标适配器根据需要加载数据,或者您可以按需说,这将解决您的问题。
http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html
进一步阐述
public class SimpleCursorAdapter extends CursorAdapter {
private LayoutInflater mLayoutInflater;
private Context mContext;
/**This is your constructor, Just pass it the cursor which you got from querying
* SMS database
*
* @param context
* @param c
*/
public SimpleCursorAdapter(Context context, Cursor c) {
super(context, c);
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
/**
* Write here code for binding you view to data form cursor
*
* Just like any other adapter, fetch all the columns details you want
* and set those values properly
*/
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
/**
* Just inflate your child view layout and return it to system
*/
View v = mLayoutInflater.inflate(R.layout.<YOUR_VIEW_NAME>, parent, false);
return v;
}
}
多数民众赞成,一旦你完成上述事情,就可以从你的活动中得到一个光标 从sms db,创建一个这个适配器的实例,就像任何设置它到列表视图。