您好我正在开发一个Android应用程序,我试图在列表视图上显示通话记录。
private void getCallDetails()
{
Cursor managedCursor = getContentResolver().query( CallLog.Calls.CONTENT_URI,null, null,null, "date DESC");
int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER );
int type = managedCursor.getColumnIndex( CallLog.Calls.TYPE );
int date = managedCursor.getColumnIndex( CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex( CallLog.Calls.DURATION);
int nameid = managedCursor.getColumnIndex( CallLog.Calls.CACHED_NAME);
while (managedCursor.moveToNext())
{
HashMap<String, String> map = new HashMap<String, String>();
phNumber = managedCursor.getString( number );
//type of call
String callType = managedCursor.getString( type );
//call date
String callDate = managedCursor.getString( date );
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM");
String dateString = formatter.format(new Date(Long.valueOf(callDate)));
//call time
SimpleDateFormat time = new SimpleDateFormat("HH:mm");
String timeString = time.format(new Date(Long.valueOf(callDate)));
String calldur = managedCursor.getString(duration);
int sec = Integer.parseInt(calldur);
//converting seconds
String dur = String.format("%02d:%02d:%02d", sec/3600, (sec%3600)/60, (sec%60));
String name = managedCursor.getString(nameid);
if(name == null)
{
map.put(contactName, "Unknown");
}
else
{
map.put(contactName, name);
}
int dir = 0;
int imageId = 0;
int dircode = Integer.parseInt( callType );
switch( dircode )
{
case CallLog.Calls.OUTGOING_TYPE:
dir = R.drawable.outgoing;
imageId = R.drawable.clock;
callDuration = dur;
break;
case CallLog.Calls.INCOMING_TYPE:
dir = R.drawable.incoming;
imageId = R.drawable.clock;
callDuration = dur;
break;
case CallLog.Calls.MISSED_TYPE:
dir = R.drawable.missed;
callDuration = " ";
imageId = R.drawable.empty;
break;
}
map.put(phone, phNumber);
map.put(DateOfCall,dateString );
map.put(TimeOfCall,timeString );
dirType = String.valueOf(dir);
map.put(typeofCall,dirType );
map.put(durationOfCall, callDuration);
clkimgId = String.valueOf(imageId);
map.put(imgId,clkimgId);
map.put(image,contactID);
DetailsList.add(map);
}
managedCursor.close();
}
我在doInBackground中调用上面的getCallDetails()并在onPostExecute中加载到适配器
adapter = new Adapter(Cal.this, DetailsList);
callList.setAdapter(adapter);
但是这个加载速度太慢了。我该如何解决?不知道我哪里错了。
请帮助。 谢谢!
答案 0 :(得分:0)
公共类CallSmsSafeService扩展了Service {
private class MyPhoneListener extends PhoneStateListener{
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
String mode = dao.findMode(incomingNumber);
if("1".equals(mode)||"3".equals(mode)){
Log.i(TAG,"这是黑名单号码,挂断电话。。。");
//监视呼叫记录的生成,如果呼叫记录产生了。删除呼叫记录。
Uri url = Uri.parse("content://call_log/calls");
getContentResolver().registerContentObserver(url, true, new CallLogObserver(new Handler(), incomingNumber));
}
break;
}
}
}
private class CallLogObserver extends ContentObserver{
private String incomingNumber;
public CallLogObserver(Handler handler, String incomingNumber) {
super(handler);
this.incomingNumber = incomingNumber;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
getContentResolver().unregisterContentObserver(this);
deleteCalllog(incomingNumber);
}
}
@Override
public void onDestroy() {
unregisterReceiver(receiver);
receiver = null;
tm.listen(listener, PhoneStateListener.LISTEN_NONE);
listener = null;
super.onDestroy();
}
/**
* 清除呼叫记录
* @param incomingNumber
*/
public void deleteCalllog(String incomingNumber) {
ContentResolver resolver = getContentResolver();
Uri url = Uri.parse("content://call_log/calls");
resolver.delete(url, "number=?", new String[]{incomingNumber});
}
}