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

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.

Related

Dynamic SQLite queries

I'm trying to implement dynamic queries in my Android app, to let the users search according to some criteria. In this case I'm trying to search simply by an integer value. Here's my attempt:
...
public String[][] listarNegocio(int idProyecto,
int minimo,
int maximo)
{
String[][] arrayDatos = null;
String[] parametros = {String.valueOf(idProyecto)};
Cursor cursor = null;
cursor = querySQL("SELECT *" +
" FROM negocio" +
" WHERE ? in (0, id_proyecto)", parametros);
if(cursor.getCount() > 0)
{
int i = minimo - 1;
arrayDatos = new String[maximo - minimo + 1][20];
while(cursor.moveToNext() && i < maximo)
{
// Here I fill the array with data
i = i + 1;
}
}
cursor.close();
CloseDB();
return(arrayDatos);
}
public Cursor querySQL(String sql, String[] selectionArgs)
{
Cursor oRet = null;
// Opens the database object in "write" mode.
db = oDB.getReadableDatabase();
oRet = db.rawQuery(sql, selectionArgs);
return(oRet);
}
...
I tested this query using SQLFiddle, and it should return only the rows where the column id_proyecto equals the parameter idProyecto, or every row if idProyecto equals 0. But it doesn't return anything. If I remove the WHERE clause and replace "parametros" with "null", it works fine.
Additionally, I need to search by text values, using LIKE. For example, WHERE col_name LIKE strName + '%' OR strName = ''. How should I format my parameters and the query to make it work?
You should do one query for each case. For an id that exists, do SELECT * FROM negocio WHERE id_proyecto = ?. For an id that doesn't exist (I'm assuming 0 isn't a real id), just query everything with SELECT * FROM negocio.
Code should be something like this:
if(parametros[0] != 0){
cursor = querySQL("SELECT *" +
" FROM negocio" +
" WHERE id_proyecto = ?", parametros);
} else {
cursor = querySQL("SELECT *" +
" FROM negocio", null);
}
Regarding your second question, it depends on what you're looking for, you could use LIKE '%param%' or CONTAINS for occurrences in between text, LIKE param for partial matches or just = param if you're looking an exact match.

Unable to find Contact by Number due to Formatting difference

It's supposed to be a common problem but I don't know what is the right word to look for solution.
In my app, I user can key in a number, and I need to search if the contact exist in database. Also, given a contact ID and number, I want to find the phone label of the number..
The problem is, if I have a number in phone as "8710 2863" and my query arg is "87102863", I won't be able to get the contact and phone label... There must be a way to standardize the format.. But I can't find the way :(
Here is my code:
Cursor cursor = MyApp.getContext().getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.LABEL
},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND "
+ ContactsContract.CommonDataKinds.Phone.NUMBER + " = ? ",
new String[] { Long.toString(contactId), number },
ContactsContract.Data.IS_SUPER_PRIMARY + " DESC");
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
int type = cursor.getInt(0);
String label = cursor.getString(1);
String phoneLabel = (String) ContactsContract.CommonDataKinds.Phone
.getTypeLabel(MyApp.getContext().getResources(), type, label);
hashBuilder.put(Defs.ARG_PHONE_TYPE, phoneLabel);
}
} finally {
cursor.close();
}
}
Have you tried PhoneNumberUtils.formatNumber(String)?

How to get recently dialled numbers

I am working on an application, where i need to fetch 5 recently dialled numbers. I am able to sort the numbers in descending order, so as the recently dialled numbers comes first. I am using the following code to fetch the recent numbers.
dialledCall = (TextView)findViewById(R.id.dialledCall);
String strOrder = CallLog.Calls.DATE + " DESC";
Cursor mCallCursor = getApplicationContext().getContentResolver().query(CallLog.Calls.CONTENT_URI,
null,
CallLog.Calls.TYPE
,null,
strOrder);
mCallCursor.moveToFirst();
do {
if( i >5 )
break;
mobileNumber = mCallCursor.getString(mCallCursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
number += mobileNumber + "\n";
i++;
}
while (mCallCursor.moveToNext());
dialledCall.setText(number);
With this, i am able to sort the numbers in recent order and also i have just fetched last 5 numbers. But the query returns all the numbers, i mean Dialled, Missed and Received as well. But here i want just the dialled numbers.
I know there is something to do with CallLog.Calls.TYPE in the above query.
I have tried passing null value, but it returns all the numbers.
I also have tried CallLog.Calls.OUTGOING_TYPE in place of it, but it gives error that int value is not permitted, it should be a string value.
Any suggestions on how to replace the above code, so as to only get the dialled calls.
Note- I have already looked at https://stackoverflow.com/a/10480569/1626878, but it does not provide a solution to only fetch the dialled numbers.
You can use the CallLog class to get at this information (see http://developer.android.com/reference/android/provider/CallLog.Calls.html).
You're interested in the getLastOutgoingCall method, which returns the last phone number called.
Check the call logs. You can easily access this. Do something like,
Define,
public Cursor mCallCursor;
Then define the fields you want to obtain from the call log,
public static final String[] STR_FIELDS = {
android.provider.CallLog.Calls.NUMBER,
android.provider.CallLog.Calls.TYPE,
android.provider.CallLog.Calls.CACHED_NAME,
android.provider.CallLog.Calls.CACHED_NUMBER_TYPE,
android.provider.CallLog.Calls.DATE,
android.provider.CallLog.Calls.DURATION, android.provider.CallLog.Calls.CACHED_NUMBER_LABEL,android.provider.CallLog.Calls.NUMBER
};
Set the order,
public static final String STR_ORDER = android.provider.CallLog.Calls.DATE + " DESC";
Call the cursor.
mCallCursor = getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI,
STR_FIELDS,
null,
null,
STR_ORDER);
I have solved the issue, so I am posting it for future visitors.
String mobileNumber="";
int i = 1;
String number = "";
TextView dialledCall;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dialledCall = (TextView)findViewById(R.id.dialledCall);
// String[] strFields = { android.provider.CallLog.Calls.NUMBER, android.provider.CallLog.Calls.TYPE, android.provider.CallLog.Calls.CACHED_NAME, android.provider.CallLog.Calls.CACHED_NUMBER_TYPE, android.provider.CallLog.Calls.DURATION };
String strOrder = CallLog.Calls.DATE + " DESC";
Cursor mCallCursor = getApplicationContext().getContentResolver().query(CallLog.Calls.CONTENT_URI,
null,
CallLog.Calls.TYPE + " = " + CallLog.Calls.OUTGOING_TYPE
,null,
strOrder);
mCallCursor.moveToFirst();
do {
if( i >5 )
break;
mobileNumber = mCallCursor.getString(mCallCursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
number += mobileNumber + "\n";
i++;
}
while (mCallCursor.moveToNext());
dialledCall.setText(number);
}
Here CallLog.Calls.TYPE + " = " + CallLog.Calls.OUTGOING_TYPE in the query separates out the dialled calls.

SQLite select issue

I have written method which should return string with data from select query, but it doesn't work perfectly as I would like, here is method:
public String selectorDanych(String kolumna, String log){
String test = "Select "+ kolumna + " from "+ Usr_TABLE+ " where "+colLogin + " ='" +log+"';";
Cursor cursor2 = sqLiteDatabase.rawQuery(test, null);
return cursor2.toString();
}
it returns some kind of string but, it is not the thing I want [ the string it returns is something like "SQLite.database.#" etc
You're returning the internal name of the Cursor you got from the query, not any data from the query results.
You should use something like:
cursor2.moveToFirst(); // position the cursor at the first returned row
String col = cursor2.getString(the_index_of_the_column_you_want);
cursor2.close();
return col;
Make sure you test for errors though (there might be no rows returned at all), and read the Cursor API docs.
Rewrite your code as
public String selectorDanych(String kolumna, String log){
String test = "Select "+ kolumna + " from "+ Usr_TABLE+ " where "+colLogin + " ='" +log+"';";
Cursor cursor2 = sqLiteDatabase.rawQuery(test, null);
//Write these codes
if(cursor2.moveToFirst()) {
cursor2.close(); //You should close your cursor
return cursor2.getString(0); //index of your kolumna field
} else {
cursor2.close(); //You should close your cursor
return null; //Return some error msg, to notify that data not found
}
}
You can use the code below. You may catch exception after the try block. Even if you don't, you're guaranteed that the close() on the cursor will be called! I also suggest to always use English names for your vars.
public String selectorDanych(String kolumna, String log){
String test = "Select "+ kolumna + " from "+ Usr_TABLE+ " where "+colLogin + " ='" +log+"';";
Cursor cursor2 = sqLiteDatabase.rawQuery(test, null);
if (cursor2 != null && cursor2.moveToFirst()) {
try { //use try - finally to close the cursor in the finally block
int index_kolumna = cursor2.getColumnIndexOrThrow(kolumna);
String kolumna_val = cursor2.getString(index_kolumna);
} finally {
if (cursor2 != null && !cursor2.isClosed()) {
cursor2.close();
}
}
}

retrieve contact's nickname

I want to get the nickname of a contact from addressbook. I start with his phone number, query it and want the nickname (aka alias) as a result.
Cursor cur = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.NUMBER + " = " + incomingNumber, null, null);
if (cur.moveToFirst()) {
Log.e("saymyname", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME)));
Log.e("saymyname", cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.LABEL)));
}
Output of the logs is the incomingNumber (first Log.e() ) and null (second Log.e() ), but I want to get the contact's nickname!
Thanks
Tom
Nickname is held in a different table than the phone numbers, you have to query ContactsContract.Data.CONTENT_URI
Check my answer on this question
(I don't have necessary reputation to comment so I have to add answer)
TomTasche's answer is misleading and it got me to waste a lot of time trying to figure out why I couldn't get the proper nickname on a contact I knew had one.
I found the answer by myself but got the confirmation from this post that I was now doing it properly.
Basically when you read the ContactsContract.Data documentation you read:
Each row of the data table is typically used to store a single piece of contact information (such as a phone number) and its associated metadata (such as whether it is a work or home number).
That explains the shady part of TomTasche's code :
if (nickname.equals(incomingNumber)) {
return name;
}
Since doing a search with just the CONTACT_ID can return multiple rows (one for each information type), it's not guaranteed that the first one contains the nickname. When there is a nickname it will be in DATA1, but the phone number is also found in DATA1.
The example part in the documentation of ContactsContract.Data makes it clear that rows must be selected based on their MIME_TYPE, only when the MIME_TYPE is selected, can we start making sense of the content in the row itself.
A proper query would therefore be:
cursor = getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
new String[]{Nickname.NAME},
ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + "= ?",
new String[]{contactID, Nickname.CONTENT_ITEM_TYPE},
null);
(where Nickname is ContactsContract.CommonDataKinds.Nickname)
I felt I had to say something on this topic to prevent other people wasting as much time as I did based on this sole topic (first one I found with my Google friend).
The answer from Pentium10 was very helpful! Thanks!
If anybody needs a sample, look at the following code:
public String accessContact(String incomingNumber) {
if (incomingNumber == null || "".equals(incomingNumber)) {
return "unknown";
}
try {
Cursor cur = context.getContentResolver().query(Phone.CONTENT_URI, new String[] {Phone.DISPLAY_NAME, Phone.TYPE, Phone.CONTACT_ID}, Phone.NUMBER + " = " + incomingNumber, null, null);
int nameIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int typeIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
int idIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
String name;
String type;
String id;
if (cur.moveToFirst()) {
name = cur.getString(nameIndex);
type = cur.getString(typeIndex);
id = cur.getString(idIndex);
} else {
return "unknown";
}
cur = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[] {ContactsContract.Data.DATA1}, ContactsContract.Data.CONTACT_ID + " = " + id, null, null);
int nicknameIndex = cur.getColumnIndex(ContactsContract.Data.DATA1);
String nickname;
if (cur.moveToFirst()) {
nickname = cur.getString(nicknameIndex);
if (nickname.equals(incomingNumber)) {
return name;
}
return nickname;
} else {
return name;
}
} catch (Exception e) {
e.printStackTrace();
return "unknown";
}

Categories

Resources