0

昨天我正在寻找一种获取安卓设备通话记录的方法。

我的想法是获取所有可能的东西,然后解析它,只得到我真正需要的东西。

按照文档查看 CallLog.Calls 文档,我看到了不同的字段,但是在尝试获取它们时,由于文档的差异而导致错误。

4

1 回答 1

0

最后我让它工作了,所以我想分享代码以防其他人需要它。

在代码中,我使用 JSON 对象并将它们保存在 Documents 文件夹中。

private void getCallLog(Context context) {
    @SuppressLint("MissingPermission") Cursor cursor = getApplicationContext().getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null,
            null, CallLog.Calls.DATE + " DESC");
    if (cursor.getCount() > 0) {
        try{
            // Get the directory for the user's public pictures directory.
            final File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
            // Make sure the path directory exists.
            if (!path.exists()) {
                // Make it, if it doesn't exit
                path.mkdirs();
            }
            final File file = new File(path, "call.txt");

            file.createNewFile();
            FileOutputStream fOut = new FileOutputStream(file);
            OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);


            while (cursor.moveToNext()) {
                try {
                    JSONObject object = new JSONObject();
                    object.put("ANSWERED_EXTERNALLY_TYPE", CallLog.Calls.ANSWERED_EXTERNALLY_TYPE);
                    object.put("BLOCKED_TYPE", CallLog.Calls.BLOCKED_TYPE);
                    object.put("CACHED_FORMATTED_NUMBER", cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_FORMATTED_NUMBER)));
                    object.put("CACHED_LOOKUP_URI", cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_LOOKUP_URI)));
                    object.put("CACHED_MATCHED_NUMBER", cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_MATCHED_NUMBER)));
                    object.put("CACHED_NAME", cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME)));
                    object.put("CACHED_NORMALIZED_NUMBER", cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NORMALIZED_NUMBER)));
                    object.put("CACHED_NUMBER_LABEL", cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NUMBER_LABEL)));
                    object.put("CACHED_NUMBER_TYPE", cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NUMBER_TYPE)));
                    object.put("CACHED_PHOTO_ID", cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_PHOTO_ID)));
                    object.put("CACHED_PHOTO_URI", CallLog.Calls.CACHED_PHOTO_URI); // Wrong docs
                    object.put("CONTENT_ITEM_TYPE", CallLog.Calls.CONTENT_ITEM_TYPE); // Wrong docs
                    object.put("CONTENT_TYPE", CallLog.Calls.CONTENT_TYPE); // Wrong docs
                    object.put("COUNTRY_ISO", cursor.getString(cursor.getColumnIndex(CallLog.Calls.COUNTRY_ISO)));
                    object.put("DATA_USAGE", cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATA_USAGE)));
                    object.put("DATE", cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE)));
                    object.put("DEFAULT_SORT_ORDER", CallLog.Calls.DEFAULT_SORT_ORDER); // Wrong docs
                    object.put("DURATION", cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION)));
                    object.put("EXTRA_CALL_TYPE_FILTER", CallLog.Calls.EXTRA_CALL_TYPE_FILTER); // Wrong docs
                    object.put("FEATURES", cursor.getString(cursor.getColumnIndex(CallLog.Calls.FEATURES)));
                    object.put("FEATURES_HD_CALL", CallLog.Calls.FEATURES_HD_CALL);
                    object.put("FEATURES_PULLED_EXTERNALLY", CallLog.Calls.FEATURES_PULLED_EXTERNALLY);
                    object.put("FEATURES_VIDEO", CallLog.Calls.FEATURES_VIDEO);
                    object.put("FEATURES_WIFI", CallLog.Calls.FEATURES_WIFI);
                    object.put("GEOCODED_LOCATION", cursor.getString(cursor.getColumnIndex(CallLog.Calls.GEOCODED_LOCATION)));
                    object.put("INCOMING_TYPE", CallLog.Calls.INCOMING_TYPE);
                    object.put("IS_READ", cursor.getString(cursor.getColumnIndex(CallLog.Calls.IS_READ)));
                    object.put("LAST_MODIFIED", CallLog.Calls.LAST_MODIFIED); // Wrong docs
                    object.put("LIMIT_PARAM_KEY", CallLog.Calls.LIMIT_PARAM_KEY); // Wrong docs
                    object.put("MISSED_TYPE", CallLog.Calls.MISSED_TYPE);
                    object.put("NEW", cursor.getString(cursor.getColumnIndex(CallLog.Calls.NEW)));
                    object.put("NUMBER", cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER)));
                    object.put("NUMBER_PRESENTATION", cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER_PRESENTATION)));
                    object.put("OFFSET_PARAM_KEY", CallLog.Calls.OFFSET_PARAM_KEY); // Wrong docs
                    object.put("OUTGOING_TYPE", CallLog.Calls.OUTGOING_TYPE);
                    object.put("PHONE_ACCOUNT_COMPONENT_NAME", cursor.getString(cursor.getColumnIndex(CallLog.Calls.PHONE_ACCOUNT_COMPONENT_NAME)));
                    object.put("PHONE_ACCOUNT_ID", cursor.getString(cursor.getColumnIndex(CallLog.Calls.PHONE_ACCOUNT_ID)));
                    object.put("POST_DIAL_DIGITS", CallLog.Calls.POST_DIAL_DIGITS); // Wrong docs
                    object.put("PRESENTATION_ALLOWED", CallLog.Calls.PRESENTATION_ALLOWED);
                    object.put("PRESENTATION_PAYPHONE", CallLog.Calls.PRESENTATION_PAYPHONE);
                    object.put("PRESENTATION_RESTRICTED", CallLog.Calls.PRESENTATION_RESTRICTED);
                    object.put("PRESENTATION_UNKNOWN", CallLog.Calls.PRESENTATION_UNKNOWN);
                    object.put("REJECTED_TYPE", CallLog.Calls.REJECTED_TYPE);
                    object.put("TRANSCRIPTION", cursor.getString(cursor.getColumnIndex(CallLog.Calls.TRANSCRIPTION)));
                    object.put("TYPE", cursor.getString(cursor.getColumnIndex(CallLog.Calls.TYPE)));
                    object.put("VIA_NUMBER", CallLog.Calls.VIA_NUMBER); // Wrong docs
                    object.put("VOICEMAIL_TYPE", CallLog.Calls.VOICEMAIL_TYPE); // Wrong docs
                    object.put("VOICEMAIL_URI", cursor.getString(cursor.getColumnIndex(CallLog.Calls.VOICEMAIL_URI)));
                    myOutWriter.append(object.toString());
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            myOutWriter.close();
            fOut.flush();
            fOut.close();
        } catch (Exception e) {
            Log.e("Exception", "File write failed: " + e.toString());
        }

    }
}
于 2018-05-16T12:37:23.243 回答