Capture text from Sms when sending it

0

In the code snippet below I capture the message and the phone that is received by sms on the cell phone and compare the message with a string already saved and in the same case I execute an action responding to the phone number, all using BroadcastReceiver, I I would like to do the same for sent messages, so that when the message is sent and the same match the string, execute the same action, using the same class. Searching on just found ContentObserver content to capture sent messages, in which it appears to be out of date, I soon run out of ideas.

public class SmsReceiver extends BroadcastReceiver {
    private final static String TAG = SmsReceiver.class.getSimpleName();

    public SmsReceiver() {

    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String keyword = PreferenceManager.getDefaultSharedPreferences(context).getString("keyword", "");

        if (keyword.length() == 0) {
            //Log.d(TAG, "No keyword available. Exit");
            return;
        }

        ArrayList<SmsMessage> list = null;
        try {
            list = getMessagesWithKeyword(keyword, intent.getExtras());
        } catch (Exception e) {
            return;
        }

        if (list.size() == 0) {
            //Log.d(TAG, "No message available. Exit");
            return;
        }

        if (!Permissions.haveSendSMSAndLocationPermission(context)) {
            try {
                Permissions.setPermissionNotification(context);
            } catch (Exception e) {
                Toast.makeText(context, R.string.send_sms_and_location_permission, Toast.LENGTH_SHORT).show();
            }

            return;
        }

        Intent newIntent = new Intent(context, SmsSenderService.class);
        newIntent.putExtra("phoneNumber", list.get(0).getOriginatingAddress());
        context.startService(newIntent);
    }

    private ArrayList<SmsMessage> getMessagesWithKeyword(String keyword, Bundle bundle) {
        ArrayList<SmsMessage> list = new ArrayList<SmsMessage>();
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            for (int i = 0; i < pdus.length; i++) {
                SmsMessage sms = null;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    String format = bundle.getString("format");
                    sms = SmsMessage.createFromPdu((byte[]) pdus[i], format);
                } else {
                    sms = SmsMessage.createFromPdu((byte[]) pdus[i]);
                }

                if (sms.getMessageBody().toString().equals(keyword)) {
                    list.add(sms);
                }
            }
        }
        return list;
    }
}
    
asked by anonymous 12.02.2018 / 17:37

0 answers