我的设备上有超过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.
答案 0 :(得分: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();