Reputation: 843
I am interested in how I can figure out if SMS has been sent from the device.
In order to get notification when SMS is recieved, we use a broadcaster with:
android.provider.Telephony.SMS_RECEIVED
Important to mention that I do not send SMS from my app, I just should listen when SMS is sent from the device.
May be I should listen to some Content provider (which somehow related with SMS) and react for that change. Any ideas how I can achieve that?
Upvotes: 9
Views: 15976
Reputation: 132972
Yes, It is possible to listen SMS ContentProvider by using ContentObserver
Here is my example for Outgoing SMS:
First register a ContetObserver with content://sms/
public class Smssendservice extends Service{
@Override
public void onCreate() {
SmsContent content = new SmsContent(new Handler());
// REGISTER ContetObserver
this.getContentResolver().
registerContentObserver(Uri.parse("content://sms/"), true, SMSObserver);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
SMSObserver.class
public class SMSObserver extends ContentObserver {
private Handler m_handler = null;
public SMSObserver(SMSLogger handler){
super(handler);
m_handler = handler;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(bSelfChange);
Uri uriSMSURI = Uri.parse("content://sms");
Cursor cur = this.getContentResolver().query(uriSMSURI, null, null,
null, null);
cur.moveToNext();
String protocol = cur.getString(cur.getColumnIndex("protocol"));
if(protocol == null) {
//the message is sent out just now
}
else {
//the message is received just now
}
}
}
}
Upvotes: 16
Reputation: 3657
One thing that comes to mind is using the logcat log. Every time a SMS is sent/received there is a specific entry. Just hook up your phone in debug and test and you will observe the specific entry.
I should mention this method is done from your app, so you can work it into your code.
Take a pull of this. There may be other methods but that seems fairly easy and do-able.
Upvotes: 1