简体   繁体   中英

Unable to delete number from call log android containing () and -

I am using following to delete a number from call log. The method works fine, but for those number which contains () and - does not work fine.

  public void deleteNumber(Context context, String mobile_number) {
        String Calls = "content://call_log/calls";

        Uri UriCalls = Uri.parse(Calls);

        Cursor cur = context.getContentResolver().query(UriCalls, null, null, null, null);

        Toast.makeText(context, "The number to be deleted is: " + mobile_number, Toast.LENGTH_LONG).show();


        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String query = "NUMBER='" + mobile_number + "'";

                int i = context.getContentResolver().delete(UriCalls, query, null);

                if (i > 0) {
                    Toast.makeText(context, "The number " + mobile_number + " is deleted.", Toast.LENGTH_LONG).show();

                    break;
                }

            }

        }

        cur.close();

    }

Number which are saved in this format are not deleted. Please help me to solve this.

Edit :

The following did the trick:

mobile_number = mobile_number.replaceAll("[^0-9]","");

I am using a method, which returns a mobile number. This number contains all characters including (), spaces etc. It need to be filtered.

在此处输入图片说明

Replace:

            String query = "NUMBER='" + mobile_number + "'";

            int i = context.getContentResolver().delete(UriCalls, query, null);

with:

            String query = "NUMBER=?";
            String[] args={ mobile_number };
            int i = context.getContentResolver().delete(UriCalls, query, args);

and see if you have better luck. ( and ) are probably reserved characters and would need to be escaped. Using query parameters would solve that for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM