问题
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
回答1:
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
, the Uri
you register for must be the base Uri
for the SMS Provider. That is, change Uri.parse("content://sms/out")
to Uri.parse("content://sms")
. If you want to handle only outgoing messages, you will have to query the Provider in the onChange()
method, and retrieve the type
column value for the message, checking for a value of 2
, which indicates a sent message.
If you're supporting an API level lower than 16, then it would be something like this:
private static final Uri uri = Uri.parse("content://sms");
private static final String COLUMN_TYPE = "type";
private static final int MESSAGE_TYPE_SENT = 2;
...
@Override
public void onChange(boolean selfChange) {
Cursor cursor = null;
try {
cursor = resolver.query(uri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int type = cursor.getInt(cursor.getColumnIndex(COLUMN_TYPE));
if (type == MESSAGE_TYPE_SENT) {
// Sent message
}
}
}
finally {
if (cursor != null)
cursor.close();
}
}
Starting with API 16, the ContentObserver
class offers an onChange()
overload that will provide the specific Uri
for the message as the second parameter, which you can query and inspect more efficiently than the base Uri
. 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"
.
来源:https://stackoverflow.com/questions/34692384/android-catching-outgoing-sms-using-contentobserver-or-receiver-not-working