以编程方式获取Android中传入和传出呼叫的通话记录

发布于 2024-12-05 17:14:04 字数 55 浏览 1 评论 0原文

我正在制作一个应用程序,我想在其中获取所有来电、去电和未接来电的通话记录。我怎样才能做到这一点?

I am making an app in which I want to get the call logs of all incoming, outgoing and missed calls. How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

帅气尐潴 2024-12-12 17:14:04

这里的所有答案都使用现在已弃用的 managementQuery 。应将其替换为 getContext().getContentResolver().query() 方法,如此处所述< /a> 并在此处进行了演示。

这是基于这些示例的简短示例代码:

String[] projection = new String[] {
                CallLog.Calls.CACHED_NAME,
                CallLog.Calls.NUMBER,
                CallLog.Calls.TYPE,
                CallLog.Calls.DATE
        };
// String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

 Cursor cursor =  mContext.getContentResolver().query(CallLog.Calls.CONTENT_URI, projection, null, null, null);
 while (cursor.moveToNext()) {
    String name = cursor.getString(0);
    String number = cursor.getString(1);
    String type = cursor.getString(2); // https://developer.android.com/reference/android/provider/CallLog.Calls.html#TYPE
    String time = cursor.getString(3); // epoch time - https://developer.android.com/reference/java/text/DateFormat.html#parse(java.lang.String
    }
cursor.close();

All the answers here are using managedQuery which is now deprecated. It should be replaced with getContext().getContentResolver().query() method instead, as mentioned here and demonstrated here.

Here is a short sample code, based on those examples:

String[] projection = new String[] {
                CallLog.Calls.CACHED_NAME,
                CallLog.Calls.NUMBER,
                CallLog.Calls.TYPE,
                CallLog.Calls.DATE
        };
// String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

 Cursor cursor =  mContext.getContentResolver().query(CallLog.Calls.CONTENT_URI, projection, null, null, null);
 while (cursor.moveToNext()) {
    String name = cursor.getString(0);
    String number = cursor.getString(1);
    String type = cursor.getString(2); // https://developer.android.com/reference/android/provider/CallLog.Calls.html#TYPE
    String time = cursor.getString(3); // epoch time - https://developer.android.com/reference/java/text/DateFormat.html#parse(java.lang.String
    }
cursor.close();
爱人如己 2024-12-12 17:14:04

请参考以下链接:

获取 Android 电话通话记录/以编程方式记录

如果您对上面的链接有疑问,请点击此处

Please refer the following link:

Get Android phone call history/log programmatically

If you have issues with the link above, please click here.

东风软 2024-12-12 17:14:04

此代码适用于我:

private void getCallDetails() {

    StringBuffer sb = new StringBuffer();
    Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, null, null, null);
    int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
    int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
    int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
    int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
    sb.append("Call Details :");
    while (managedCursor.moveToNext()) {
        String phNumber = managedCursor.getString(number); // mobile number
        String callType = managedCursor.getString(type); // call type
        String callDate = managedCursor.getString(date); // call date
        Date callDayTime = new Date(Long.valueOf(callDate)); 
        String callDuration = managedCursor.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;
        }
        sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration);
        sb.append("\n----------------------------------");
    }
    managedCursor.close();
    miss_cal.setText(sb);
    Log.e("Agil value --- ", sb.toString());
}

注意:如果您想获取特定的呼叫类型,请使用以下代码。例如,如果我想要单独的收入呼叫,则在开关中命令/删除相同的代码:

case beneath

然后,在收入呼叫案例中使用以下代码:

sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration);               
sb.append("\n-----Agil----------------------------------");

This code works for me:

private void getCallDetails() {

    StringBuffer sb = new StringBuffer();
    Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, null, null, null);
    int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
    int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
    int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
    int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
    sb.append("Call Details :");
    while (managedCursor.moveToNext()) {
        String phNumber = managedCursor.getString(number); // mobile number
        String callType = managedCursor.getString(type); // call type
        String callDate = managedCursor.getString(date); // call date
        Date callDayTime = new Date(Long.valueOf(callDate)); 
        String callDuration = managedCursor.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;
        }
        sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration);
        sb.append("\n----------------------------------");
    }
    managedCursor.close();
    miss_cal.setText(sb);
    Log.e("Agil value --- ", sb.toString());
}

Note: If you want to get the particular call type, then use the below code. For example, if I want income call alone then command/remove the same code in the switch:

case beneath

Then, use the below code inside income call case:

sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration);               
sb.append("\n-----Agil----------------------------------");
黑凤梨 2024-12-12 17:14:04
public class MainActivity extends Activity 
{
TextView textView = null;


int callcode;
String callType ;
String phNum;
Date callDate;
String callTypeCode;
String strcallDate;
String callDuration;
String currElement;
static boolean ring = false;
static boolean callReceived = false;


StringBuffer sb = new StringBuffer();

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.textview_call);


                SmsManager sms = SmsManager.getDefault();
                String strOrder = android.provider.CallLog.Calls.DATE + " DESC";

                /* Query the CallLog Content Provider */
                @SuppressWarnings("deprecation")
                Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,
                        null, null, strOrder);

                int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);

                int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);

                int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);

                int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);

                sb.append("Call Log :");



                if(managedCursor.moveToFirst())
                {
                    String phNum = managedCursor.getString(number);

                    String callTypeCode = managedCursor.getString(type);

                    String strcallDate = managedCursor.getString(date);

                    Date callDate = new Date(Long.valueOf(strcallDate));

                    String callDuration = managedCursor.getString(duration);

                    String callType = null;

                    int callcode = Integer.parseInt(callTypeCode);

                    switch (callcode) 
                    {
                    case CallLog.Calls.OUTGOING_TYPE:


                        callType = "Outgoing";



                        //sms.sendTextMessage(phNum, null, "Outgoing msg",  null, null);  


                        break;
                    case CallLog.Calls.INCOMING_TYPE:

                        callType = "Incoming";
                        //sms.sendTextMessage(phNum, null, "Incoming msg",  null, null);  


                        break;
                    case CallLog.Calls.MISSED_TYPE:

                        callType = "Missed";
                    //sms.sendTextMessage(phNum, null, "Missed msg",  null, null);  

                        break;
                    }
                    sb.append("\nPhone Number:--- " + phNum + " \nCall Type:--- "
                            + callType + " \nCall Date:--- " + callDate
                        + " \nCall duration in sec :--- " + callDuration);
                    sb.append("\n----------------------------------");
                }


                managedCursor.close();
            textView.setText(sb);

}

}    
public class MainActivity extends Activity 
{
TextView textView = null;


int callcode;
String callType ;
String phNum;
Date callDate;
String callTypeCode;
String strcallDate;
String callDuration;
String currElement;
static boolean ring = false;
static boolean callReceived = false;


StringBuffer sb = new StringBuffer();

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.textview_call);


                SmsManager sms = SmsManager.getDefault();
                String strOrder = android.provider.CallLog.Calls.DATE + " DESC";

                /* Query the CallLog Content Provider */
                @SuppressWarnings("deprecation")
                Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,
                        null, null, strOrder);

                int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);

                int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);

                int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);

                int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);

                sb.append("Call Log :");



                if(managedCursor.moveToFirst())
                {
                    String phNum = managedCursor.getString(number);

                    String callTypeCode = managedCursor.getString(type);

                    String strcallDate = managedCursor.getString(date);

                    Date callDate = new Date(Long.valueOf(strcallDate));

                    String callDuration = managedCursor.getString(duration);

                    String callType = null;

                    int callcode = Integer.parseInt(callTypeCode);

                    switch (callcode) 
                    {
                    case CallLog.Calls.OUTGOING_TYPE:


                        callType = "Outgoing";



                        //sms.sendTextMessage(phNum, null, "Outgoing msg",  null, null);  


                        break;
                    case CallLog.Calls.INCOMING_TYPE:

                        callType = "Incoming";
                        //sms.sendTextMessage(phNum, null, "Incoming msg",  null, null);  


                        break;
                    case CallLog.Calls.MISSED_TYPE:

                        callType = "Missed";
                    //sms.sendTextMessage(phNum, null, "Missed msg",  null, null);  

                        break;
                    }
                    sb.append("\nPhone Number:--- " + phNum + " \nCall Type:--- "
                            + callType + " \nCall Date:--- " + callDate
                        + " \nCall duration in sec :--- " + callDuration);
                    sb.append("\n----------------------------------");
                }


                managedCursor.close();
            textView.setText(sb);

}

}    
披肩女神 2024-12-12 17:14:04
 private void getLastNumber() {

       //this help you to get recent call
        Uri contacts = CallLog.Calls.CONTENT_URI;
        Cursor managedCursor = context.getContentResolver().query(contacts, null, null, 
                       null, android.provider.CallLog.Calls.DATE + " DESC limit 1;");
        int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
        int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
        int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
        int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);

        StringBuffer sb = new StringBuffer();
        managedCursor.moveToNext();
        String phNumber = managedCursor.getString(number);
        String callType = managedCursor.getString(type);
        String callDate = managedCursor.getString(date);
        String callDayTime = new Date(Long.valueOf(callDate)).toString();
        int callDuration = managedCursor.getInt(duration);
        managedCursor.close();

        int dircode = Integer.parseInt(callType);
       sb.append("Phone Number:--- " + phNumber + " ,Call Date:--- " + callDayTime + " ,Call duration in sec :--- " + callDuration);
        sb.append("\n----------------------------------");
        Log.d("calllogs", "getLastNumber: "+"Phone Number:--- " + phNumber + " ,Call Date:--- " + callDayTime + " ,Call duration in sec :--- " + callDuration);
    }
 private void getLastNumber() {

       //this help you to get recent call
        Uri contacts = CallLog.Calls.CONTENT_URI;
        Cursor managedCursor = context.getContentResolver().query(contacts, null, null, 
                       null, android.provider.CallLog.Calls.DATE + " DESC limit 1;");
        int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
        int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
        int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
        int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);

        StringBuffer sb = new StringBuffer();
        managedCursor.moveToNext();
        String phNumber = managedCursor.getString(number);
        String callType = managedCursor.getString(type);
        String callDate = managedCursor.getString(date);
        String callDayTime = new Date(Long.valueOf(callDate)).toString();
        int callDuration = managedCursor.getInt(duration);
        managedCursor.close();

        int dircode = Integer.parseInt(callType);
       sb.append("Phone Number:--- " + phNumber + " ,Call Date:--- " + callDayTime + " ,Call duration in sec :--- " + callDuration);
        sb.append("\n----------------------------------");
        Log.d("calllogs", "getLastNumber: "+"Phone Number:--- " + phNumber + " ,Call Date:--- " + callDayTime + " ,Call duration in sec :--- " + callDuration);
    }
简单 2024-12-12 17:14:04

你可以试试这个

private void getCallLogWithDuration() {
    StringBuffer sb = new StringBuffer();
    Uri contacts = CallLog.Calls.CONTENT_URI;
    Cursor managedCursor = this.getContentResolver().query(contacts, null, null, null, null);
    int name = managedCursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
    int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
    int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
    int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
    int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
    sb.append("Call Details :");
    while (managedCursor.moveToNext()) {

        HashMap rowDataCall = new HashMap<String, String>();

        String mName = managedCursor.getString(name);
        String phNumber = managedCursor.getString(number);
        String callType = managedCursor.getString(type);
        String callDate = managedCursor.getString(date);
        String callDayTime = new SimpleDateFormat("EEEE dd MMM yyyy HH:mm:s").format(new Date(Long.parseLong(callDate)));
        // long timestamp = convertDateToTimestamp(callDayTime);
        String callDuration = managedCursor.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;
        }
        sb.append("\nName:--- " + mName + "\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration);
        sb.append("\n----------------------------------");


    }
    managedCursor.close();
    System.out.println(sb);
    Log.i("CallLog", sb.toString());
}

You can try this

private void getCallLogWithDuration() {
    StringBuffer sb = new StringBuffer();
    Uri contacts = CallLog.Calls.CONTENT_URI;
    Cursor managedCursor = this.getContentResolver().query(contacts, null, null, null, null);
    int name = managedCursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
    int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
    int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
    int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
    int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
    sb.append("Call Details :");
    while (managedCursor.moveToNext()) {

        HashMap rowDataCall = new HashMap<String, String>();

        String mName = managedCursor.getString(name);
        String phNumber = managedCursor.getString(number);
        String callType = managedCursor.getString(type);
        String callDate = managedCursor.getString(date);
        String callDayTime = new SimpleDateFormat("EEEE dd MMM yyyy HH:mm:s").format(new Date(Long.parseLong(callDate)));
        // long timestamp = convertDateToTimestamp(callDayTime);
        String callDuration = managedCursor.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;
        }
        sb.append("\nName:--- " + mName + "\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration);
        sb.append("\n----------------------------------");


    }
    managedCursor.close();
    System.out.println(sb);
    Log.i("CallLog", sb.toString());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文