I have been developing the application which can manage a device using SMS commands; the application must execute commands from SMS and delete this Sms. So, the code for managing works, but I can’t delete the SMS. I have following BroadcastReceiver:
public class SmsReceiver extends BroadcastReceiver {
private static final Uri SMS_INBOX_URI = Uri.parse("content://sms");
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String body = "";
String number = "";
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (bundle != null) {
// ---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
body += msgs[i].getMessageBody().toString();
number +=msgs[i].getOriginatingAddress();
}
}
SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(context);
String key=prefs.getString(context.getString(R.string.acc_key_key), "");
if (body.trim().equals("#"+key)) {
sendGPSCoordinates(context, number);
} else if (body.trim().equals("?"+key)){
sendResponces(context);
}
deleteSms(body, number, context);
}
private void deleteSms(String body, String number, Context context) {
ContentResolver resolver=context.getContentResolver();
int count=resolver.delete(SMS_INBOX_URI, "body = ? AND address = ?", new String[]{body, number});
Log.e("count", String.valueOf(count));
/* Cursor results=resolver.query(SMS_INBOX_URI, null, null, null, null);
for (String str:results.getColumnNames()) {
Log.e("column", str);
}
}
and I have AndroidManifest.xml with needed permissions. The application receives the SMS, gets the SMS data from an Intent, manage the phone and tries to delete this SMS, but it always returns count is equals 0. I have tried to change URI for sms/inbox, but it doesn’t works. Please, tell me, how can I do it? Thank you.
I assume, that you know, that an SMS-Message is send as ordered-Broadcast. So your Reciever has the ability to get them BEFORE the native SMS-app will get it (just set a higher priority inside Manifest than the native one). Therefore it should work to just
cancelthe Broadcast being send, that the SMS never reaches the native sms-app.You could try it like this: