【问题标题】:Loading SMS in doInBackgroung too slow在 doInBackgroung 中加载 SMS 太慢
【发布时间】:2013-11-20 05:54:39
【问题描述】:

您好,我正在开发一个 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();}
} 

是否可以在 onPreExecute 加载大约 10 条 SMS 并在 doInBackground 中继续加载其他的?我尝试了类似的东西。但它不起作用。谁能指导我解决这个加载缓慢的问题。

请帮忙。

谢谢!

【问题讨论】:

    标签: android android-asynctask progressdialog android-contentresolver


    【解决方案1】:

    尝试使用光标适配器,而不是先读取所有短信,然后再显示它们。光标适配器根据需要加载数据,或者您可以说按需加载,这将解决您的问题。

    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,创建这个适配器的一个实例,就像任何将它设置到您的列表视图一样。

    【讨论】:

    • 抱歉请假,无法回复您。你能指导我使用光标适配器加载 SMS 的任何链接或项目吗?
    • 它非常简单且易于实现,就像任何其他适配器一样,只是将数据源更改为数据库。您可以查看以下链接以获取有关使用它们的更多信息。 vogella.com/articles/AndroidSQLite/article.html
    • 好的。试一试。如果您有 SMS cursorAdapter 的示例项目或链接,请分享
    猜你喜欢
    • 2017-08-16
    • 2021-05-30
    • 2019-01-19
    • 2018-04-05
    • 1970-01-01
    • 2019-05-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多