Listen outgoing SMS or sent box in Android
Basically, you have to register a content observer... something like this:
ContentResolver contentResolver = context.getContentResolver();
contentResolver.registerContentObserver(Uri.parse("content://sms/out"),true, yourObserver);
yourObserver
is an object (new YourObserver(new Handler())
) that could look like this:
class YourObserver extends ContentObserver {
public YourObserver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
// save the message to the SD card here
}
}
So, how exactly do you get the content of the SMS? You must use a Cursor
:
// save the message to the SD card here
Uri uriSMSURI = Uri.parse("content://sms/out");
Cursor cur = this.getContentResolver().query(uriSMSURI, null, null, null, null);
// this will make it point to the first record, which is the last SMS sent
cur.moveToNext();
String content = cur.getString(cur.getColumnIndex("body"));
// use cur.getColumnNames() to get a list of all available columns...
// each field that compounds a SMS is represented by a column (phone number, status, etc.)
// then just save all data you want to the SDcard :)
Android: Is there any way to listen outgoing sms?
You will have to do something like this:
- Cache all messages's hash code on the phone
- Register an content observer for content://sms
- In onChange method of observer, enumrate all messages to check if it is in cache, if not, the message is sent out just now.
Good luck with your project :-)
Edit: md5 method
You can take the (arrival date + message) text to get a unique md5 output.
private String md5(String in) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance("MD5");
digest.reset();
digest.update(in.getBytes());
byte[] a = digest.digest();
int len = a.length;
StringBuilder sb = new StringBuilder(len << 1);
for (int i = 0; i < len; i++) {
sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
sb.append(Character.forDigit(a[i] & 0x0f, 16));
}
return sb.toString();
} catch (NoSuchAlgorithmException e) { e.printStackTrace(); }
return null;
}
Listening to outgoing sms not working android
In addition to the silly mistake on my part that @CommonsWare pointed out, me missing
@Override
public void onCreate() {
super.onCreate();
ContentResolver contentResolver = this.getContentResolver();
contentResolver.registerContentObserver(Uri.parse("content://sms/out"),true, new SmsObserver(new Handler()));
}
the other issue was actually in these two lines
contentResolver.registerContentObserver(Uri.parse("content://sms/out"),true, new SmsObserver(new Handler()));
and
Uri uriSMSURI = Uri.parse("content://sms/out");
where I changed the content://sms/out
to content://sms
and then proceeded to check for the type to make sure the change I was getting was actually an outgoing message. I also had to make sure to check for multiple calls like so:
if (cr.getInt(cr.getColumnIndex("type")) == 2) {
if(id != cr.getInt(cr.getColumnIndex(cr.getColumnName(0)))) {
id = cr.getInt(cr.getColumnIndex(cr.getColumnName(0)));
String address = cr.getString(cr.getColumnIndex("address"));
Database.messageSent(SmsOutgoingObserver.this, address);
Log.d("OUTGOING", address);
} else {
Log.d("OUTGOING", "MESSAGE ALREADY LOGGED");
}
};
}
and simply defined private int id = 0;
as a global variable
Intercepting Outgoing SMS
Incoming SMS
You can intercept an incoming sms thru sms listener using Broadcast receiver.You can modify the incoming sms or destroy it so that it does not reaches inbox.
Outgoing SMS
You can listen for outgoing sms by putting content observer
over content://sms/out
but you can not modify it with the native sms app.You can obviously modify the content of content://sms/out
but it has no point.
Receive broadcast when send an sms
I found the answer
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(VIEW_RESOURCE_ID);
SendSmsObserver smsObeserver = (new SendSmsObserver(new Handler()));
ContentResolver contentResolver = this.getContentResolver();
contentResolver.registerContentObserver(Uri.parse("content://sms"),true, smsObeserver);
}
public class SendSmsObserver extends ContentObserver {
public SendSmsObserver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
// save the message to the SD card here
Log.d("sent sms", "one text send");
}
}
Related Topics
Using Disklrucache in Android 4.0 Does Not Provide for Opencache Method
Android View Not Attached to Window Manager
Android: How to Check If a Particular Accessibilityservice Is Enabled
Android: Collapsingtoolbarlayout and Swiperefreshlayout Get Stuck
Avoid Button Multiple Rapid Clicks
Wrong Requestcode in Onactivityresult
Android: Can Height of Slidingdrawer Be Set with Wrap_Content
How to Generate Buildconfigfield with String Type
When to Use a Content Provider
How to Hide Underbar in Edittext
What's the Difference Between Setwebviewclient VS. Setwebchromeclient
Action_View Intent for a File with Unknown Mimetype
How to Set Cursor Position in Edittext
How to Set Image from Url for Imageview
What Does It Mean to Inflate a View from an Xml File