开发者

Get the hour of the calling from Android CallLog

Hey, I want to get the hour of the calling from the Android CallLog. It is, the exactly hour开发者_JAVA技巧 that the user start the calling and if possible when he finishes the calling. Someone knows how to?

Thanks!


Here is complete solution (though not tested) - just scheme:

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());
    }
}


Focused on the original question, this is a solution, based on @barmaley's:

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));
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜