I want to catch sms messages when phone send an sms on my android application. How can i catch outgoing sms ?
I tried to use contentResolver but sometimes it does not work.
Thank you
I want to catch sms messages when phone send an sms on my android application. How can i catch outgoing sms ?
I tried to use contentResolver but sometimes it does not work.
Thank you
How can i catch outgoing sms ?
You don't.
I tried to use contentResolver but sometimes it does not work.
There is no requirement for a sent SMS to be available in any ContentProvider
. Apps that use SmsManager
directly may not have their SMS messages visible to anything other than the OS and the recipient of the message.
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.
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 :)