Search code examples
androidsmsmms

SMS Pending Intent get the _id of the SMS or MMS that was sent


Currently i'm able to send SMS and MMS messages with no problem. My issue is that when I have a PendingIntent for the sent confirmation, how can I get the _id of the message that was sent? Is there a way to refer to that one sms? I'm doing the below:

    Intent sentIntent = new Intent(id_value);
    PendingIntent sentPendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, sentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    context.registerReceiver(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String result = "";

            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    break;
                default:
                    break;
            }
        }

    }, new IntentFilter(id_value));

In the above example, id_value is a random value I generate to identify which SMS or MMS was sent. But I want to know, what is the _id of the message in the SMS and MMS db's?


Solution

  • There is a uri key in the extras for the intent. You can get it like so:

        context.registerReceiver(new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK: {
                        Bundle bundle = intent.getExtras();
                        String uri = bundle.getString("uri");
                    }
                        break;
    
                    default: {
                    }
                        break;
                }
            }
    
        }, new IntentFilter(id));
    

    The uri in this case will be like content://sms/7384. In this case the 7834 is the _id field. Using a content resolver we can get the details of that SMS or MMS.

    This is a part of my sending pending intent.

    Keep in mind that if you send a multipart SMS that only one call back will have the uri. The rest will have null for that field.