安卓(Android) 怎么获取所有通话记录

2019年10月19日11:04:29安卓(Android) 怎么获取所有通话记录已关闭评论 229 1719字阅读5分43秒

1.简介

安卓(Android) 怎么获取所有通话记录?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static Uri getAllCallLogs(ContentResolver cr, Uri internal,
            Context context, String timeStamp) {
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yy HH:mm");
        String[] callLogArray = new String[3];
        String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
        Uri callUri = Uri.parse("content:
        Cursor cur = cr.query(callUri, null, null, null, strOrder);

        FileOutputStream fOut = null;
        try {
            fOut = context
                    .openFileOutput("call_logs_" + timeStamp + ".txt",
                            Context.MODE_PRIVATE);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        OutputStreamWriter osw = new OutputStreamWriter(fOut);

        while (cur.moveToNext()) {
            callLogArray[0] = cur.getString(cur
                    .getColumnIndex(android.provider.CallLog.Calls.NUMBER));
            callLogArray[1] = cur
                    .getString(cur
                            .getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));

            int thirdIndex = cur
                    .getColumnIndex(android.provider.CallLog.Calls.DATE);
            long seconds = cur.getLong(thirdIndex);
            String dateString = formatter.format(new Date(seconds));
            callLogArray[2] = dateString;

            writeToOutputStreamArray(callLogArray, osw);
        }

        try {
            osw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return internal;
    }

    private static void writeToOutputStreamArray(String[] array,
            OutputStreamWriter oswriter) {
        for (int i = 0; i < array.length; i++) {
            try {
                oswriter.append(array[i] + "  ");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            oswriter.append("\n");
            oswriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 
继续阅读
编程十万个怎么办