如何删除最近24小时android的通话记录

时间:2018-04-23 06:06:05

标签: java android calllog

public void delete() {

    String strUriCalls = "content://call_log/calls";

    Uri UriCalls = Uri.parse(strUriCalls);

    Cursor cc = getContext().getContentResolver().query(UriCalls, null, null, null, null);

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

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

    if (cc.getCount() <= 0)

    {
        Toast.makeText(getContext(), "Call log empty", Toast.LENGTH_SHORT).show();

    }

    while (cc.moveToNext()) {


        String callNumber = cc.getString(number);





        String callDate = cc.getString(date);
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        try {
            systemDate = Calendar.getInstance().getTime();
            String myDate1 = sdf.format(systemDate);


//txtCurrentTime.setText(myDate);

            cDate = sdf.format(Long.parseLong(callDate));

            Date1 = sdf.parse(myDate1);
            Date2 = sdf.parse(cDate);
//to get time diff between current date and call date 


            millse = Date1.getTime() - Date2.getTime();
            mills = Math.abs(millse);
// to change the return value into specific time format


            long hh = (mills / (1000 * 60 * 60));
            Mins = (int) (mills / (1000 * 60)) % 60;
            long Secs = (int) (mills / 1000) % 60;
            long timeDifDays = mills / (24 * 60 * 60 * 1000);



            if (timeDifDays >= 24) {

                int i = getContext().getContentResolver().delete(UriCalls, callNumber, null);

                if (i >= 1)

                {

                    Toast.makeText(getContext(), "Number deleted", Toast.LENGTH_SHORT).show();
                } else

                {

                    Toast.makeText(getContext(), "No such number in call logs", Toast.LENGTH_SHORT).show();

                }
            }
        } catch (Exception e) {

        }
    }


    }

如果一条记录满足条件,它会删除特定号码的所有记录,我只想删除满意的记录。

1 个答案:

答案 0 :(得分:0)

public void delete(){

String strUriCalls = "content://call_log/calls";

Uri UriCalls = Uri.parse(strUriCalls);

Cursor cc = getContext().getContentResolver().query(UriCalls, null, null, null, null);

int number = cc.getColumnIndex(CallLog.Calls._ID);

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

if (cc.getCount() <= 0)

{
    Toast.makeText(getContext(), "Call log empty", Toast.LENGTH_SHORT).show();

}

while (cc.moveToNext()) {


    String callNumber = cc.getString(number);





    String callDate = cc.getString(date);
    sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    try {
        systemDate = Calendar.getInstance().getTime();
        String myDate1 = sdf.format(systemDate);

// txtCurrentTime.setText(数值指明MyDate);

        cDate = sdf.format(Long.parseLong(callDate));

        Date1 = sdf.parse(myDate1);
        Date2 = sdf.parse(cDate);

//获取当前日期和通话日期之间的时间差异

        millse = Date1.getTime() - Date2.getTime();
        mills = Math.abs(millse);

//将返回值更改为特定的时间格式

        long hh = (mills / (1000 * 60 * 60));
        Mins = (int) (mills / (1000 * 60)) % 60;
        long Secs = (int) (mills / 1000) % 60;
        long timeDifDays = mills / (24 * 60 * 60 * 1000);



        if (timeDifDays >= 24) {

            int i = getContext().getContentResolver().delete(UriCalls, BaseColumns._ID+"=?", new String[]{callNumber});

            if (i >= 1)

            {

                Toast.makeText(getContext(), "Number deleted", Toast.LENGTH_SHORT).show();
            } else

            {

                Toast.makeText(getContext(), "No such number in call logs", Toast.LENGTH_SHORT).show();

            }
        }
    } catch (Exception e) {

    }
}


}