有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何删除最近24小时安卓的通话记录

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) 个答案

  1. # 1 楼答案

    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) {
    
        }
    }
    
    
    }