如何获取两个日期之间的呼叫日志数据和日期之间传入呼叫的​​总数

时间:2016-11-23 13:05:50

标签: java android sql

我希望在今天,昨天,过去七天和过去30天之前获取通话记录历史记录以及我想要显示该特定号码的来电和去电的总持续时间。 我试过各种类型,但没有一个工作。我该怎么做才能获得输出?我不知道如何使用查询从特定日期获取通话记录。帮帮我谢谢

公共类CallLogActivity扩展了Activity {

TextView textView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_call_log);
    textView = (TextView) findViewById(R.id.textCallBetween);
    textView.setVisibility(View.GONE);
   // listcallLog = (ListView) findViewById(R.id.calllogItems);

    getCalldetails();


}

public void getCalldetails() {
    StringBuffer stringBuffer = new StringBuffer();

    String strOrder = android.provider.CallLog.Calls.DATE + " DESC";

    Calendar calender = Calendar.getInstance();

    calender.set(2016, calender.NOVEMBER, 18);
    String fromDate = String.valueOf(calender.getTime());

    calender.set(2016, Calendar.NOVEMBER, 20);
    String toDate = String.valueOf(calender.getTimeInMillis());

    String[] whereValue = {fromDate,toDate};


    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, android.provider.CallLog.Calls.DATE + " BETWEEN ? AND ?", whereValue, strOrder);

//   Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, android.provider.CallLog.Calls.DATE, new String[]{" BETWEEN ? AND ?"}, 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);

    stringBuffer.append("Call Log :");

    while (managedCursor.moveToNext())
    {
        String phoneNumber = managedCursor.getString(number);
        String callType = managedCursor.getString(type);
        String callDate = managedCursor.getString(date);
        SimpleDateFormat formatter = new SimpleDateFormat(
                "dd-MMM-yyyy HH:mm");
        String dateString = formatter.format(new Date(Long
                .parseLong(callDate)));
      //  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 = "INCOMMING";
                break;
            case CallLog.Calls.MISSED_TYPE:
                dir = "MISSED CALL";
                break;

        }

        stringBuffer.append("\nPhone Number:--- " + phoneNumber + "\nCall Type:--- "
                + dir + "\nCall Date:---"
                + dateString + "\nCall Duration:---" + callDuration);
        stringBuffer.append("\n--------------------------");

    }

    textView.setText(stringBuffer);
    textView.setVisibility(View.VISIBLE);

}`

}

2 个答案:

答案 0 :(得分:0)

一步一步走。首先以这种方式获取所有通话记录历史记录。

Uri allCalls = Uri.parse("content://call_log/calls");
Cursor c = managedQuery(allCalls, null, null, null, null);

String num= c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));// for  number
String name= c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME));// for name
String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION));// for duration
int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));// for call type, Incoming or out going.

答案 1 :(得分:0)

以下代码是在两个日期之间获取通话记录数据的解决方案。

public class CallLogActivity extends Activity {


TextView textView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_call_log);
    textView = (TextView) findViewById(R.id.textCallBetween);
    textView.setVisibility(View.GONE);
   // listcallLog = (ListView) findViewById(R.id.calllogItems);

    getCalldetails();


}

public void getCalldetails() {
    StringBuffer stringBuffer = new StringBuffer();

    String strOrder = android.provider.CallLog.Calls.DATE + " DESC";

    Calendar calender = Calendar.getInstance();


    calender.set(2016, calender.NOVEMBER, 18);
    String fromDate = String.valueOf(calender.getTimeInMillis());

    calender.set(2016, calender.NOVEMBER, 20);
    String toDate = String.valueOf(calender.getTimeInMillis());

    String[] whereValue = {fromDate,toDate};


    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, android.provider.CallLog.Calls.DATE + " BETWEEN ? AND ?", whereValue, strOrder);

//   Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, android.provider.CallLog.Calls.DATE, new String[]{" BETWEEN ? AND ?"}, 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);

    stringBuffer.append("Call Log :");

    while (managedCursor.moveToNext())
    {
        String phoneNumber = managedCursor.getString(number);
        String callType = managedCursor.getString(type);
        String callDate = managedCursor.getString(date);
        SimpleDateFormat formatter = new SimpleDateFormat(
                "dd-MMM-yyyy HH:mm");
        String dateString = formatter.format(new Date(Long
                .parseLong(callDate)));
      //  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 = "INCOMMING";
                break;
            case CallLog.Calls.MISSED_TYPE:
                dir = "MISSED CALL";
                break;

        }

        stringBuffer.append("\nPhone Number:--- " + phoneNumber + "\nCall Type:--- "
                + dir + "\nCall Date:---"
                + dateString + "\nCall Duration:---" + callDuration);
        stringBuffer.append("\n--------------------------");

    }

    textView.setText(stringBuffer);
    textView.setVisibility(View.VISIBLE);

}

}