Delete Last SMS - android

Using this code:
Uri uriSMSURI = Uri.parse("content://sms");
Cursor cur = mContext.getContentResolver().query(uriSMSURI, null, null, null,null);
String phoneNo, msg;
if (cur.moveToFirst()) {
String pid = cur.getString(1);
// do some process
Toast.makeText(mContext, pid, Toast.LENGTH_LONG).show();
Cursor cur2 = mContext.getContentResolver().query(Uri.parse("content://sms/conversations/" + pid), null, null, null,null);
if(cur2.moveToFirst()){
////Tried with this with no luck, its Delete the whole Conversation :
String pid2 = cur2.getString(1);
String uri = "content://sms/conversations/" + pid2;
mContext.getContentResolver().delete(Uri.parse(uri), null, null);
}
}
I am able to Delete the last(whole) Conversation, but I am looking to delete the Last SMS in the last conversation only, not the whole conversation itself, which way to do it?

Problem Solved,
Code :
Uri uriSMSURI = Uri.parse("content://sms/");
Cursor cur = mContext.getContentResolver().query(uriSMSURI, null, null, null, null);
if (cur.moveToFirst()) {
////Changed to 0 to get Message id instead of Thread id :
String MsgId= cur.getString(0);
mContext.getContentResolver().delete(Uri.parse("content://sms/" + MsgId), null, null);
Thanks

Related

Xamarin.Android: Delete SMS after sending

I want to send a SMS and delete the message afterwards (within an hour or two, to be able to check the delivery state).
This is the code I use for sending SMS:
SmsManager.Default.SendTextMessage(contact.Text, null,
message.Text, null, null);
How can I delete the message? I found a code for deleting SMS as below :
ContentResolver.Delete(Android.Net.Uri.Parse("content://sms/" + pid), null, null);
but I don't know how to get pid of sent message.
Try this
void deletSms(string message, string number)
{
Uri uriSms = Uri.Parse("content://sms/sent");
Android.Database.ICursor c = ContentResolver.Query(uriSms, new string[] {"_id", "thread_id","address",
"person", "date", "body" }, null, null, null);
if (c != null)
{
do
{
long id = c.GetLong(0);
long threadId = c.GetLong(1);
string address = c.GetString(2);
string body = c.GetString(5);
if (message.Equals(body) && address.Equals(number))
{
Log.Info(tag, "Deleting SMS with id: " + threadId);
ContentResolver.Delete(
Uri.Parse("content://sms/" + id), null, null);
}
} while (c.MoveToNext());
}
}

how to fetch local phone contacts effeciently

I am using the below code to fetch the local phone contacts. It is working fine and also fetching the contacts very fast.
But the problem comes here, in my contact list there are few contacts that are having multiple Emails and multiple phone numbers.
In case of multiple phone or emails address it repeats the name of the same person multiple times.
And if i change
ContactsContract.CommonDataKinds.Email.CONTENT_URI
to ContactsContract.CommonDataKinds.Phone.CONTENT_URI then it will repeat name according to the number of phone number exists for a contact. Please help
private static final String[] PROJECTION = new String[]{
ContactsContract.CommonDataKinds.Email.CONTACT_ID,
ContactsContract.CommonDataKinds.Nickname.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Phone.NUMBER,
};
ContentResolver cr = mContext.getContentResolver();
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, null, null, null);
if (cursor != null) {
try {
final int contactIdIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID);
final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
final int emailIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
final int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
long contactId;
String displayName, email, phone, photo;
while (cursor.moveToNext()) {
mNK_UserModel = new NK_Contact();
contactId = cursor.getLong(contactIdIndex);
displayName = cursor.getString(displayNameIndex);
//Adding display name
mNK_UserModel.setFirstName(displayName);
Util.DEBUG_LOG(1, "contact", "contact id :" + contactId);
al_PhoneContacts.add(mNK_UserModel);
}
} finally {
cursor.close();
}
}
If i had to guess i would say you are missing a "break" in your while-loop. Since the cursor tries to fetch the next available column entry. But have a look at my solution which worked for me in the past.
It uses a seperate cursor for each row which gives you more control over the data.
Map<String, String> contactDataMap = new HashMap<String, String>();
Uri contactData = data.getData();
Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
cursor.moveToFirst();
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
contactDataMap.put(NAME, (name != null)?name:"");
if (Integer.parseInt(cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id},
null);
while (pCur.moveToNext()) {
String number = pCur.getString(pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactDataMap.put(PHONE, (number != null)?number:"");
break;
}
pCur.close();
}
Cursor emailCur = getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCur.moveToNext()) {
String email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
contactDataMap.put(MAIL, (email != null)?email:"");
break;
}
emailCur.close();
cursor.close();

How to read "Contact Name" from inbox messages

I'm reading sms messages from inbox using content provider. See following code:
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor cursor = this.getContentResolver().query(uriSms, null, null, null, SORT_ORDER);
Inside loop I'm fetching all required columns:
String body= cursor.getString(cursor.getColumnIndex("body"));
String person = cursor.getString(cursor.getColumnIndex("person"));
I'm getting all values properly but person's name is returning 'null' value. Please guide me why contact name is not being returned from above statement.
The person column's value, if it exists, is not the sender's name. It's an ID that you would use to query the Contacts Provider to get the name. If you're getting null for person, then you'll have to use the address, which is the phone number, to query against. For example:
String address = cursor.getString(cursor.getColumnIndex("address"));
final String[] projection = new String[] {ContactsContract.Data.DISPLAY_NAME};
String displayName = null;
Cursor contactCursor = null;
try {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(address));
contactCursor = getContentResolver().query(uri,
projection,
null,
null,
null);
if (contactCursor != null && contactCursor.moveToFirst()) {
displayName = contactCursor.getString(
contactCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
}
}
catch(Exception e) {
e.printStackTrace();
}
finally {
if(contactCursor != null) {
contactCursor.close();
}
}
Try this:
Cursor cursor = getContentResolver().query(
uriSms,
new String[] { "_id", "thread_id", "address", "person", "date",
"body", "type" }, null, null, null);

Reading a specific contact from its ID on android database

I need to set the cursor to a specific contact that i have its id
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null,null, null, null);
I have that code for all the database, how can I optimize it?
I need the cursor to return the name and the default number of the contact
thank you
You actually need two cursors. One for name & id and another one for phone number.
Cursor c = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (c.moveToFirst()) {
name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
fullName = c.getString(c.getColumnIndex("display_name_alt"));
id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer.parseInt(c.getString(c.getColumnIndex
(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
//phone number
Cursor phones = getContentResolver().query(Phone.CONTENT_URI,
null,
Phone.CONTACT_ID + " = " + id,
null,
null);
while (phones.moveToNext()) {
String number_type = phones.getString(phones.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.TYPE));
if(number_type.equalsIgnoreCase("1"))
number1 = phones.getString(phones.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER));
if(number_type.equalsIgnoreCase("2"))
number2 = phones.getString(phones.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
}
c.close();
cursor = getContentResolver().query(Phone.CONTENT_URI,
null, Phone.CONTACT_ID + "=?", new String[] { id },
Phone.DISPLAY_NAME + " ASC");

How to get contactname from phonenumber please help me

How to get contactname from phonenumber please help me
Code i used:
public String contactname(String phonenumber)
{
ContentValues contentValues = new ContentValues();
Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL,
phonenumber);
Cursor cur = managedQuery(contactUri, null, null, null, null);
int nameColumn = cur.getColumnIndex(People.NAME);
String name = cur.getString(nameColumn);
return name;
}
It shows error
the type Contacts.People.Phones is deprecated, i think you should directly use Contacts, Beside, the string you set phonenumber is only used to match various parts of contact name.
maybe you can query all of the contacts first, and then match the one by giving the phonenumber.
Uri uri = Contacts.CONTENT_URI;
Cursor cursor = managedQuery(uri, null, null, null, null);
cursor.moveToFirst();
String name = null;
while (curcor.getPosition != cursor.getCount) {
if (cursor.getString(cursor.getColumnIndex("default_tel")).equals(phonenumber)) {
name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));
break;
}
}
return name;

Categories

Resources