android调用日志ContentResolver

时间:2014-09-20 09:29:27

标签: android

我正在尝试使用getContentResolver()检索android中的调用日志。

我收到以下错误

Cannot make a static reference to the non-static method getContentResolver() from the type ContextWrapper

你能解释一下吗。我刚刚开始使用android和java。

public static void GetCalls() {
    String[] CallDetails =  {
            android.provider.CallLog.Calls._ID,
            android.provider.CallLog.Calls.CACHED_NAME,
            android.provider.CallLog.Calls.NUMBER,
            android.provider.CallLog.Calls.DATE,
            android.provider.CallLog.Calls.DURATION,
    };
    Cursor cursor;

    cursor = getContentResolver().query(CallLog.CONTENT_URI, CallDetails, null, null,null);             
}

2 个答案:

答案 0 :(得分:0)

您有两个选择:

  1. 使您的方法非静态,这样您就可以使用getContentResolver()

  2. context项传递给您的方法,然后使用context.getContentResolver()

  3. 希望它有所帮助。

答案 1 :(得分:0)

private static String getCallDetails(Context context) {

    Cursor cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI,
            null, null, null, CallLog.Calls.DATE + " DESC");
    int number = cursor.getColumnIndex(CallLog.Calls.NUMBER);
    int type = cursor.getColumnIndex(CallLog.Calls.TYPE);
    int date = cursor.getColumnIndex(CallLog.Calls.DATE);
    int duration = cursor.getColumnIndex(CallLog.Calls.DURATION);       
    while (cursor.moveToNext()) {
        String phNumber = cursor.getString(number);
        String callType = cursor.getString(type);
        String callDate = cursor.getString(date);
        Date callDayTime = new Date(Long.valueOf(callDate));
        String callDuration = cursor.getString(duration);
        String dir = null;
        int dircode = Integer.parseInt(callType);
        switch (dircode) {
        case CallLog.Calls.OUTGOING_TYPE:
            dir = "OUTGOING";
            break;
        case CallLog.Calls.INCOMING_TYPE:
            dir = "INCOMING";
            break;

        case CallLog.Calls.MISSED_TYPE:
            dir = "MISSED";
            break;
        }
    }
    cursor.close();
    return stringBuffer.toString();
}``

Don't forget to add permission-
<android:name="android.permission.READ_CALL_LOG"/>

Hope this helps..