Set sms as read in Android - android

In my application I need to read sms coming from only a number, and when i receive it I need to set it as read automatically, without setting it in the sms android application, but from my app. How can I do?
Thanks!

Let me update this:
ContentValues values = new ContentValues();
values.put("read",true);
getContentResolver().update(Uri.parse("content://sms/"),values, "_id="+SmsMessageId, null);
SmsMessageId is _id of message, which you find in SMS database.

A short example:
Uri uri = Uri.parse("content://sms/inbox");
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
while (cursor.moveToNext()) {
// Retrieve sms
// see column "address" for comparing
// Then update the sms and set the column "read" to 1
}

Related

Android detect sms belongs to sender or receiver

I want to detect from sms list, which of messages are mine and witch of them belongs to other contacts.
For example:
Me: "body": Hi John
John "body: Hi there...
Uri uriSMSURI = Uri.parse("content://sms/");
Cursor cur = getActivity().getContentResolver().query(uriSMSURI, null, null, null, null);
while (cur.moveToNext()) {
//detect here
}
You can use the Telephony.TextBasedSmsColumns.CREATOR field to get the creator. Compare this to your address (mobile number) to distinguish yourself from the other people. You can get your number with the following:
(TelephonyManager)getSystemService(TELEPHONY_SERVICE).getLine1Number();

the _id column of MMS-SMS database

I am accessing Android MMS-SMS database:
Uri.parse("content://mms-sms/conversations?simple=true");
ContentResolver contentResolver = getContentResolver();
String[] projection = {"_id"};
Cursor cursor = contentResolver.query(uri, projection, null, null, null);
//Is the id the same as the id in corresponding SMS or MMS table?
long id = cursor.getLong(cursor.getColumnIndex("_id"));
My question is simple, is the "_id" column holds the same id value of corresponding table (for SMS "content://sms"; for MMS "content://mms") ? If it is not, how can I get the id of the message in SMS or MMS table from the cursor I got?
What I want to do is I want to use this id to query the corresponding SMS or MMS table.
Do you want fetch mms-sms by "_id" column from "content://mms-sms/conversations?simple=true"
try
Cursor cursor = getContentResolver().query(Uri.parse("content://mms-sms/conversations/"+id), new String[] {"_id","body","date","type","error_code","ct_t","_data","m_type"}, null, null, null);
"id" is value "_id" column.
I think you are doing that in wrong way. You should add your wanted values about sms to the projection like that;
String[] projection = {"Conversations._ID"};
then you can take sms id with that:
long id = cursor.getLong(0);
If you want to take another information from Sms, add your keys to projection like:
String[] projection = {"Conversations._ID", "Conversations.ADDRESS", "Conversations.BODY"};
then;
long id = cursor.getLong(0);
String address = cursor.getString(1); // tel number
String body = cursor.getString(2); // content of message
Edit: You should also check your uri and try that:
Cursor cursor = contentResolver.query(Conversations.CONTENT_URI, projection, null, null, null);
Good luck.

How to Differentiate MMS and SMS in android?

How to Differentiate MMS and SMS via MMS/SMS listeners in Android? Because I got SMS counts as in MMS counts?
I am calculating Total MMS as using following code.
Uri mmsInboxUri = Uri.parse("content://mms/inbox");
Uri mmsSentUri = Uri.parse("content://mms/sent");
Cursor mmsInboxCursor = getContentResolver().
query(mmsInboxUri ,new String[] {"_id"}, null, null);
Cursor mmsSentCursor = getContentResolver().
query(mmsSentUri ,new String[] {"_id"}, null, null);
int mmsCount = mmsInboxCursor.getCount() + mmsSentCursor.getCount();
You should check the column ct_t. If it's a MMS it should be equal to application/vnd.vap_multipart.related.

Android SMS count database

I am looking for code to get the total sent-sms count in android mobiles. If I use content://sms/sent/ for querying the database I get the sms count of the available sent sms in the mobile, but I want the total sms that is sent from buying the mobile.
Retrieve All SMS messages in the Android
The URI (content://sms/) will used to retrive the all SMS messages, also we can split up by the message type as well as.
Uri allMessage = Uri.parse("content://sms/");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(allMessage, null, null, null, null);
while (c.moveToNext()) {
String row = c.getString(1);
}
you can get the all inbox/outbox here
change your URI
use this one: content://sms/

writing exception to parcel exception while updating contact name in android?

I need to update a column (cached_name) of table callog.calls in sql lite database in android.what I can't figure out is how to use update statement.I am unable to find anything regarding update command using cursor.kindly help.thanks in advance.
//number1 is the phone number against which I need to run update query in db.
//name is the string which I used to insert against number1..
String name=edittext.gettext().tostring();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(CallLog.Calls.CONTENT_URI,
null, null, null, null);
ContentValues value=new ContentValues();
value.put("CallLog.Calls.CACHED_NAME",name);
cur.moveToFirst();
while(!cur.isLast())
{
String number=cur.getString(cur.getColumnIndex(CallLog.Calls.NUMBER));
if((number.equals(number1)==true)
{
try{
cr.update(CallLog.Calls.CONTENT_URI,value,CallLog.Calls.CACHED_NAME+"=?",null);
}
catch(Exception e)
{
e.printStackTrace();
System.out.println(e.getStackTrace());
}
}//if
cur.moveToNext();
}//while
Basically, the only line you actually need is the update :
String name = edittext.getText().toString();
ContentResolver cr = getContentResolver();
ContentValues value = new ContentValues();
// Note that there is no "" here. The value is a constant
value.put(CallLog.Calls.CACHED_NAME, name);
cr.update(CallLog.Calls.CONTENT_URI, values, CallLog.Calls.NUMBER+"=?",
new String[] { number1 });
This is equivalent to this raw SQL query:
UPDATE call_log SET cached_name = name WHERE number = number1;
There is no need for iterating over all the calls, that's what the where clause is for.
Also,
while(!cur.isLast())
prevents you from actually reading the last matching value. Don't do that. Use
while (cur.moveToNext())
when you need to iterate over a cursor (which you don't, here)
I strongly suggest you take a look at how cursors work, as well as how sql works.
(Also, out of curiosity, why do you need to modify the callLog table?)

Categories

Resources