1

我的设备上有超过 3000 条短信。我正在尝试读取数据库中的所有消息。我正在使用这个查询:

Cursor cur1 = c.getContentResolver().query(Uri.parse("content://sms/"), null, null, null, null);

cur1.getCount()返回所有 3000 条短信,但是当我通过循环解析它时,它只运行 400 到 500 条。

Cursor cur1 = c.getContentResolver().query(Uri.parse("content://sms/"), null, null, null, null);
int size = cur1.getCount();

if(size > 0)
{
    sms = new SMS[size];
    //int i = 0;
    for(int i = 0 ; i < size ; i++)
    {
        cur1.moveToNext();
        ContactInfo p = new ContactInfo();
        String content = cur1.getString(cur1.getColumnIndex("body"));
        String number = cur1.getString(cur1.getColumnIndex("address"));
        long date = cur1.getLong(cur1.getColumnIndex("date"));
        String person = cur1.getString(cur1.getColumnIndex("person"));
        String protocol = cur1.getString(cur1.getColumnIndex("protocol"));
        String name = p.getName(number, c);
        String type = null;

        Calendar cal=Calendar.getInstance();
        cal.clear();
        cal.setTimeInMillis(date);

        String date_time=String.format("%1$te %1$tB %1$tY,%1$tI:%1$tM:%1$tS %1$Tp",cal);

        Log.i("INFO", content+" "+i);

        sms[i] = new SMS(type , name , number , date_time , content );
    }
}

400-500 次迭代后 logcat 打印

09-19 20:28:31.148: E/liblog(3153): failed to call dumpstate
09-19 20:28:31.179: I/ActivityManager(3153): Process com.arslan (pid 1766) has died.
4

1 回答 1

0

许多其他进程正在设备上运行,并且由于读取 3000 条消息的漫长过程,它需要大量连续的 CPU 时间,这就是进程被杀死的原因。当我尝试通过线程阅读它时,它工作正常。

    final Cursor cur1 = c.getContentResolver().query(Uri.parse("content://sms/"), null, null, null, "date ASC");
    final int size = cur1.getCount();

    final int sleeptimer = size;

    final SMS [] sms = new SMS[size];


    Thread  myThread = new Thread()
    {
        public void run()
        {
            try
            {
                int currentwait = 0;
                int j=0;
                while(currentwait < sleeptimer)
                {
                    sleep(200);
                    currentwait+=200;
                    for(int i = 0 ; i < 200 ; i++)
                    {
                        if(!cur1.moveToNext())
                        {
                            break;
                        }
                        ContactInfo p = new ContactInfo();
                        String content = cur1.getString(cur1.getColumnIndex("body"));
                        String number = cur1.getString(cur1.getColumnIndex("address"));
                        long date = cur1.getLong(cur1.getColumnIndex("date"));
                        String protocol = cur1.getString(cur1.getColumnIndex("protocol"));
                        String name = p.getName(number, c);
                        String type = null;

                        Calendar cal=Calendar.getInstance();
                        cal.clear();
                        cal.setTimeInMillis(date);

                        String date_time=String.format("%1$te %1$tB %1$tY,%1$tI:%1$tM:%1$tS %1$Tp",cal);

                        Log.i("INFO", content+" "+j);

                        sms[j] = new SMS(type , name , number , date_time , content );
                        j++;
                    }
                }
            }
            catch(Exception e)
            {

            }
            finally{

            }
        }
    };
    myThread.start();
于 2012-09-20T07:02:32.750 回答