1. Permission
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
2. Source
public class MainActivity extends Activity {
public static final String MESSAGE_TYPE_INBOX = "1";
public static final String MESSAGE_TYPE_SENT = "2";
public static final String MESSAGE_TYPE_CONVERSATIONS = "3";
public static final String MESSAGE_TYPE_NEW = "new";
final static private String[] CALL_PROJECTION = { CallLog.Calls.TYPE,
CallLog.Calls.CACHED_NAME, CallLog.Calls.NUMBER,
CallLog.Calls.DATE, CallLog.Calls.DURATION };
private static final String TAG = "Victor-Manage_Clique";
private Cursor getCallHistoryCursor(Context context) {
Cursor cursor = context.getContentResolver().query(
CallLog.Calls.CONTENT_URI, CALL_PROJECTION,
null, null, CallLog.Calls.DEFAULT_SORT_ORDER);
return cursor;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
callLog();
}
private void callLog() {
int callcount = 0;
String callname = "";
String calltype = "";
String calllog = "";
Cursor curCallLog = getCallHistoryCursor(this);
Log.i( TAG , "processSend() - 1");
// Log.i( TAG , "curCallLog: " + curCallLog.getCount());
if (curCallLog.moveToFirst() && curCallLog.getCount() > 0) {
while (curCallLog.isAfterLast() == false) {
StringBuffer sb = new StringBuffer();
if (curCallLog.getString(curCallLog
.getColumnIndex(CallLog.Calls.TYPE)).equals(MESSAGE_TYPE_INBOX)){
calltype = "수신";
}
else if (curCallLog.getString(curCallLog
.getColumnIndex(CallLog.Calls.TYPE)).equals(MESSAGE_TYPE_SENT)){
calltype = "발신";
}
else if (curCallLog.getString(curCallLog
.getColumnIndex(CallLog.Calls.TYPE)).equals(MESSAGE_TYPE_CONVERSATIONS)){
calltype = "부재중";
}
if (curCallLog.getString(curCallLog
.getColumnIndex(CallLog.Calls.CACHED_NAME)) == null) {
callname = "NoName";
}
else {
callname = curCallLog.getString(curCallLog
.getColumnIndex(CallLog.Calls.CACHED_NAME));
}
sb.append(timeToString(curCallLog.getLong(curCallLog
.getColumnIndex(CallLog.Calls.DATE))));
sb.append("\t").append(calltype);
sb.append("\t").append(callname);
sb.append("\t").append(curCallLog.getString(curCallLog.getColumnIndex(CallLog.Calls.NUMBER)));
curCallLog.moveToNext();
String backupData = sb.toString();
callcount++;
Log.i("call history[", sb.toString());
}
}
}
private String timeToString(Long time) {
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = simpleFormat.format(new Date(time));
return date;
}
}
No comments:
Post a Comment