i trying to catch outgoing SMS event using content observer.
//TEST OBSERVER
ContentObserver co = new SMSoutObserver(new Handler(), getApplicationContext());
ContentResolver contentResolver = getApplicationContext().getContentResolver();
contentResolver.registerContentObserver(Uri.parse("content://sms/out"),true, co);
// END TEST OBSERVER
and
public class SMSoutObserver extends ContentObserver {
private Context mCtx;
public SMSoutObserver(Handler handler, Context ctx) {
super(handler);
mCtx = ctx;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
// save the message to the SD card here
Logger.d("On Change");
Toast.makeText(mCtx,"TEST", Toast.LENGTH_LONG).show();
}
}
But if i send outgoing message in emulator event is not triggered.
I tried use receiver too.
<receiver android:name=".receiver.SMSReceiver"
android:enabled="true"
android:exported="true"
android:priority="1000">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="android.provider.Telephony.SMS_SENT"/>
</intent-filter>
</receiver>
with receiver
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
Logger.d("SMSReceiver triggered");
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
//do something with the received sms
Logger.d("Incoming SMS");
}else if(intent.getAction().equals("android.provider.Telephony.SMS_SENT")){
//do something with the sended sms
Logger.d("Outgoing SMS");
}
// Start reminder service
// context.startService(new Intent(context, ReminderService.class));
} catch (Exception e) {
Logger.e("onReceive method cannot be processed");
TrackingEventLogHelper.logException(e, Constants.Global.EXCEPTION,
Constants.ExceptionMessage.EXC_CANNOT_CANNOT_PROCESS_REBOOT_RECEIVER, true);
}
}
}
But this receiver is triggering only for incoming messages, not for outgoing. How should i do it in the right way working on android version > 5 too.
Many thanks for any advice
There is no
"android.provider.Telephony.SMS_SENT"
action in the SDK, nor is there any system-wide broadcast upon an SMS send, so your second method isn't going to work.As for the
ContentObserver
, theUri
you register for must be the baseUri
for the SMS Provider. That is, changeUri.parse("content://sms/out")
toUri.parse("content://sms")
. If you want to handle only outgoing messages, you will have to query the Provider in theonChange()
method, and retrieve thetype
column value for the message, checking for a value of2
, which indicates a sent message.If you're supporting an API level lower than 16, then it would be something like this:
Starting with API 16, the
ContentObserver
class offers anonChange()
overload that will provide the specificUri
for the message as the second parameter, which you can query and inspect more efficiently than the baseUri
. Also, if your minimum API level is 19, there are several classes with constants that you can substitute for those defined in the example code above.As a side note, the
Uri
for the outbox is"content://sms/outbox"
, not"content://sms/out"
.