嘿,我想从Android CallLog获取通话时间。它是用户启动呼叫的正好时间,如果可能的话,当他完成呼叫时。有人知道怎么做?
谢谢!
答案 0 :(得分:1)
这是完整的解决方案(虽然没有经过测试) - 只是方案:
public void logCallLog(String number)
{
long dialed;
String columns[]=new String[] {
CallLog.Calls._ID,
CallLog.Calls.NUMBER,
CallLog.Calls.DATE,
CallLog.Calls.DURATION,
CallLog.Calls.TYPE};
String args[]=new String[1];
args[0]=number;
Cursor c;
c = this.managedQuery(Uri.parse("content://call_log/calls"),
columns, CallLog.Calls.NUMBER+"=?", args, "Calls._ID DESC"); //last record first
while (c.moveToNext())
{
dialed=c.getLong(c.getColumnIndex(CallLog.Calls.DATE));
Log.i("CallLog", "Call to number: "+number+", registered at: "+new Date(dialed).toString());
}
}
答案 1 :(得分:0)
专注于原始问题,这是一个基于@barmaley的解决方案:
public void logCallLog(String number)
{
long dialed;
String columns[]=new String[] {
CallLog.Calls._ID,
CallLog.Calls.NUMBER,
CallLog.Calls.DATE,
CallLog.Calls.DURATION,
CallLog.Calls.TYPE};
String args[]=new String[1];
args[0]=number;
Cursor c;
c = this.managedQuery(Uri.parse("content://call_log/calls"), columns, CallLog.Calls.NUMBER+"=?", args, "Calls._ID DESC"); //last record first
while (c.moveToNext())
{
int duration = c.getInt(c.getColumnIndex(CallLog.Calls.DURATION));// for duration in seconds
dialed=c.getLong(c.getColumnIndex(CallLog.Calls.DATE));
GregorianCalendar callStart = new GregorianCalendar();
callStart.setTimeInMillis(dialed);
int hstart = callStart.get(GregorianCalendar.HOUR_OF_DAY);
//using callStart and duration you can calculate the end hour:
dialed += duration+1000;
GregorianCalendar callEnd = new GregorianCalendar();
callEnd.setTimeInMillis(dialed);
int hend = callEnd.get(GregorianCalendar.HOUR_OF_DAY);
Log.i("CallLog", String.format("Start:%d End:%d", hstart, hend));
}
}