为获取呼叫日志提供服务

时间:2014-12-04 15:09:59

标签: android

我创建了一个程序,在启动后会将调用日志记录到String中,获取调用日志的函数在Service类上运行,但是当我编写代码来获取调用日志时出现错误

这是我的代码

public void callLogs(){
        Cursor mCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, null, null, null);
        StringBuffer sb = new StringBuffer();
        int number = mCursor.getColumnIndex(CallLog.Calls.NUMBER);
        int date = mCursor.getColumnIndex(CallLog.Calls.DATE);
        int duration = mCursor.getColumnIndex(CallLog.Calls.DURATION);
        int type = mCursor.getColumnIndex(CallLog.Calls.TYPE);

        mCursor.moveToLast();
        int counter = 0;
        do{
            String phnNumber = mCursor.getString(number);
            String callDuration = mCursor.getString(duration);
            String callType = mCursor.getString(type);
            String callDate = mCursor.getString(date);
            Date d = new Date(Long.valueOf(callDate));
            String callTypeStr = "";
            int dircode = Integer.parseInt(callType);
            switch (dircode){

            case  CallLog.Calls.OUTGOING_TYPE:
                callTypeStr = "Outgoing";
                break;
            case CallLog.Calls.INCOMING_TYPE:
                callTypeStr = "Incoming";
                break;
            case CallLog.Calls.MISSED_TYPE:
                callTypeStr = "Missed";
                break;
                }

        sb.append("\nPhone number :" + phnNumber);
        sb.append("\nCall Duration :" + callDuration + "sec");
        sb.append("\nCall type :" + callTypeStr);
        sb.append("\nCall date :" + d);
        sb.append("\n-------------------");
        sb.append(System.getProperty("line.separator"));

        counter++;

        }
        while(mCursor.moveToPrevious() && counter<=15);{
        }


        String finalSb = new String(sb);
}

我得到了错误 “对于TestService类型,未定义方法managedQuery(Uri,null,null,null,null)”

1 个答案:

答案 0 :(得分:0)

据我所知,您需要在managedQuery上指定“活动”。

e.g. getActivity().managedQuery...

但是,我建议你不要使用它。在API 11中弃用它,而不是CursorLoader

API Doc for ManagedQuery

在页面顶部创建一个变量,如:

Activity myAcc;

假设您的班级名称是'myClass',请创建如下函数:

public myClass(Activity acc)
{
    this.myAcc = acc;
}

然后将您的功能更改为

public void callLogs(Activity acc){
    Cursor mCursor = acc.managedQuery(CallLog.Calls.CONTENT_URI, null, null, null, null);

    ...
}

当你调用函数时:

callLogs(myAcc);