Android获取通话记录

最近在做android通讯录,看了很多资料。把我代码分享给大家。

package com.example.call;


import java.text.SimpleDateFormat;
import java.util.Date;
import com.example.contacts.R;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.CallLog.Calls;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;


public class CallActivity extends Activity {
protected static final String ACTIVITY_TAG = "MyAndroid";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call);
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, null, null,
null, null);
if (cursor.moveToFirst()) {
do {
CallLog calls = new CallLog();
// 号码
String number = cursor.getString(cursor
.getColumnIndex(Calls.NUMBER));
// 呼叫类型
String type;
switch (Integer.parseInt(cursor.getString(cursor
.getColumnIndex(Calls.TYPE)))) {
case Calls.INCOMING_TYPE:
type = "呼入";
break;
case Calls.OUTGOING_TYPE:
type = "呼出";
break;
case Calls.MISSED_TYPE:
type = "未接";
break;
default:
type = "挂断";// 应该是挂断.根据我手机类型判断出的
break;
}
SimpleDateFormat sfd = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date date = new Date(Long.parseLong(cursor.getString(cursor
.getColumnIndexOrThrow(Calls.DATE))));
// 呼叫时间
String time = sfd.format(date);
// 联系人
String name = cursor.getString(cursor
.getColumnIndexOrThrow(Calls.CACHED_NAME));
// 通话时间,单位:s
String duration = cursor.getString(cursor
.getColumnIndexOrThrow(Calls.DURATION));
Log.d(CallActivity.ACTIVITY_TAG, "联系人电话" + number + ",类型"
+ type + "时间" + time + "联系人" + name);
} while (cursor.moveToNext());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.call, menu);
return true;
}


}

你可能感兴趣的:(通讯录)