Delete SMS after displaying - android

I am writing my own SMS application that will display a toast of my message once it arrive. Now is it possible to delete the message after the display of the toast, so that it will not go into the native SMS application?
Thanks In Advance,
Perumal

Use BroadcastReceiver to trap the incoming SMS.
Read the message body and store it somewhere or show it in the Toast you mentioned.
use the following code to delete SMS's in your inbox. It will be deleted immidiately .
ContentResolver cr = _context.getContentResolver();
Uri inbox = Uri.parse( "content://sms/inbox" );
Cursor cursor = cr.query(
inbox,
new String[] { "_id", "thread_id", "body" },
null,
null,
null);do {
String body = cursor.getString( 2 );
long thread_id = cursor.getLong( 1 );
Uri thread = Uri.parse( "content://sms/conversations/" + thread_id );
cr.delete( thread, null, null );
count++;
} while ( cursor.moveToNext() );

Related

How to access miscall and new message state in android?

I wanna make an android app that runs in background and check if there is a miscall or new massage and if it is true do something.How can I run my app in background (it does not close when I click back button) and how to access miscall and new message state I am an amateur in android.
Thanks!
For not seen sms messages there is a column in content://sms that you can check in order to determine if a message has been seen.
Column name is "seen".
example of calling it and checking:
ContentResolver mContectResolver = context.getContentResolver();
Uri uri = Uri.parse("content://sms/");
String[] mProjection = {
"_id",
"address",
"person",
"date",
"body",
"protocol",
"seen"
};
Cursor cursor = mContectResolver.query(uri, mProjection, null, null, null);
cursor.moveToFirst();
for(int i = 0 ; i<cursor.getCount() ; i++){
if(cursor.getString(cursor.getColumnIndexOrThrow("seen")).equalsIgnoreCase("1")){
//Message has not been seen
}
cursor.moveToNext();
}
cursor.close();

Determine if a phone number has sms messages in android

Is there a way to tell if a phone number has an sms(s) in android ??
I want to know if there is a way to tell using the sms content porvider I know its not documented and not recommanded to use but is there a way to tell ??
what i have tried .
this statement will get all the inbox sms(s)
cursor = getContentResolver().query(
Uri.parse("content://sms/inbox"), null, null, null,
null);
I cant use the where clause like this
cursor = getContentResolver().query(
Uri.parse("content://sms/inbox"), "address =?", new String[]{"001435436654747"}, null,
null);
because the number may be stored without the global code the "001" or such .
is there a work around for this ??
Hi you can use below code to do this , I use this in my application to listen incoming sms to reply automaticaly.
Uri myMessage = Uri.parse("content://sms/");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(myMessage, new String[] {
"_id", "address", "date", "body",
"read" }, null, null, null);
System.out.println(Number);
while (c.moveToNext()) {
Number = c.getString(c.getColumnIndexOrThrow("address")).toString();
if (Number.equals("001435436654747")) {
// DO YOUR STUFF
}
}

differentiate inbox and sentsms from all sms

I am working on an ANdroid application.In my app I have to list all the conversations and i did that part.Each conversation is containing all sms to that number. So i have to differtiate inbox and sentsms from all sms.I know about the following api's can use to find inbox and sent.
content://sms/inbox
content://sms/sent
But i don't want to use this.I listed all sms by using the api
content://sms/
I tested with the columnindex's type,address but it always gives the same result for inbox and outbox.And my sample code is
Uri SMS_INBOX = Uri.parse("content://sms");
c = getContentResolver().query(SMS_INBOX, null, "thread_id" + " = "
+ "3", null,
"date" + " ASC");
if(c.moveToFirst()){
count.add(c.getCount());
for(int j=0;j<c.getCount();j++){
System.out.println(c.getString(c.getColumnIndexOrThrow("body")).toString());
System.out.println("new person=="+c.getColumnIndex("person")+"type=="+c.getColumnIndexOrThrow("type"));
c.moveToNext();
}
}
c.close();
Please help me.
You can use ContentObserver here to track sent and received message,
Override onChange() method of ContentObserver and get the sms type and work accordingly. Psuedo code can be as below.
Cursor cursor = mContext.getContentResolver().query(Uri
.parse("content://sms"), null, null, null, null);
String type = cursor.getColumnIndex("type");
if(cursor.getString(type).equalsIgnoreCase("1")){
// sms received
}
else if(cursor.getString(type).equalsIgnoreCase("2")){
//sms sent
}
Registering ContentObserver for SMS,
ContentResolver observer = this.getContentResolver();
observer.registerContentObserver(Uri.parse("content://sms"),
true, new MySMSObserver(new Handler(),this));
Where MySMSObserver will be your class extending ContentObserver with Constructor having argument as Handler and Context,
public MySMSObserver(Handler handler, Context context) {
super(handler);
this.context = context;
}

How to search word in SMS inbox within the application?

I am new to Android. I want to know how to search by word in SMS inbox within the application. Can anyone help me. Please give me the source code if you could.
** Use Content Resolver ("content://sms/inbox") to read SMS which are in inbox.**
Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
cursor.moveToFirst();
do{
String msgData = "";
for(int idx=0;idx<cursor.getColumnCount();idx++)
{
msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
}
if(msgData.contains(yourWord))
;// do something
}while(cursor.moveToNext());
This seems to have been answered several times already here and there for pointer for instance.
Please do search for existing questions before creating new ones.
first you have to query the inbox content provider for sms
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uriSMSURI,new String[] { "_id", "thread_id", "address", "person", "date", "body" }, null, null,null);
Then search iterate and get sms body
if(cur.getCount()>0) {
while(cur.moveToNext()) {
String smsBody = cur.getString(5);
Log.v("body",smsBody);
if(smsBody.contains("matching string")) {
}
also refer this link
how can i read inbox in android mobile?

Android - Querying the SMS ContentProvider?

I currently register a content observer on the following URI "content://sms/" to listen out for incoming and outgoing messages being sent.
This seems to work ok and I have also tried deleting from the sms database but I can only delete an entire thread from the following URI "content://sms/conversations/"
Here is the code I use for that
String url = "content://sms/";
Uri uri = Uri.parse(url);
getContentResolver().registerContentObserver(uri, true, new MyContentObserver(handler));
}
class MyContentObserver extends ContentObserver {
public MyContentObserver(Handler handler) {
super(handler);
}
#Override public boolean deliverSelfNotifications() {
return false;
}
#Override public void onChange(boolean arg0) {
super.onChange(arg0);
Log.v("SMS", "Notification on SMS observer");
Message msg = new Message();
msg.obj = "xxxxxxxxxx";
handler.sendMessage(msg);
Uri uriSMSURI = Uri.parse("content://sms/");
Cursor cur = getContentResolver().query(uriSMSURI, null, null,
null, null);
cur.moveToNext();
String protocol = cur.getString(cur.getColumnIndex("protocol"));
if(protocol == null){
Log.d("SMS", "SMS SEND");
int threadId = cur.getInt(cur.getColumnIndex("thread_id"));
Log.d("SMS", "SMS SEND ID = " + threadId);
Cursor c = getContentResolver().query(Uri.parse("content://sms/outbox/" + threadId), null, null,
null, null);
c.moveToNext();
int p = cur.getInt(cur.getColumnIndex("person"));
Log.d("SMS", "SMS SEND person= " + p);
//getContentResolver().delete(Uri.parse("content://sms/conversations/" + threadId), null, null);
}
else{
Log.d("SMS", "SMS RECIEVE");
int threadIdIn = cur.getInt(cur.getColumnIndex("thread_id"));
getContentResolver().delete(Uri.parse("content://sms/conversations/" + threadIdIn), null, null);
}
}
}
However I want to be able to get the recipricant and the message text from the SMS Content Provider, can anyone tell me how to do this?
And also how to delete one message instead of an entire thread?
This was already discussed.
To read SMS from the Content provider check:
- android-1-5-reading-sms-messages
Check this threads:
delete-sms-in-android-1-5
how-to-delete-sms-from-inbox-in-android-programmatically
can-we-delete-an-sms-in-android-before-it-reaches-the-inbox
About your comment saying that you are deleting a whole thread instead of a single sms:
Have you tried out this code?
The address column contains the telephone number of the sms sender.
Use cursor.getString(cursor.getColumnIndex("address")) to pull the telephone number as a String. Pop a cursor on content://sms and sort it in date descending order to get the most recent message. You will have to wait for the new message to enter the table otherwise you will pull information from the wrong message. In an incomingSMS broadcastReceiver use a while loop, in a thread, polling for the cursor.getCount() to change. Then after the while loop cursor.moveToFirst will be the new message.
For example:
Cursor cur = getContentResolver().query(Uri.parseUri(content://sms), null, null, null, null);
int count = cur.getCount();
while (cur.getCount() == count)
{
Thread.sleep(1000);
cur = getContentResolver().query(Uri.parseUri(content://sms), null, null, null, null);
}
Then get the address of the sms sender:
cur = getContentResolver().query(Uri.parseUri(content://sms), null, null, null, "date DESC");
cur.MoveToFirst();
String telephoneNumber = cur.getString(cur.getColumnIndex("address");
This while loop will pause the thread until the new message arrives. You can also use a contentObserver, but this while loop is simple and does not require registration, unregistration, and a separate class.
Frankly I think its faster to pull the address and the body of the message directly from the pdu of the incoming intent. This way you dont have to wait for the message to enter the table to get the address and the body. The Android class SmsMessage has a variety of useful methods.

Categories

Resources