1

I have already know that how to read the messages from inbox but I want to implement a android app to read only transaction message and display it in a list view with transaction amount ,credit debit etc.For my complete code. current complete code for fetching sms data.how to filter the sms data according to requirement.

public List<SmsInfo> getSmsInfo() {
        String[] projection = new String[] { "_id", "address", "person",
                "body", "date", "type" };

//      @SuppressWarnings("deprecation")
//      Cursor cursor = activity.managedQuery(uri, projection, null, null,
//              "date desc");

        ContentResolver cr = activity.getContentResolver();
        Cursor cursor = cr.query(uri, projection, null, null, "date desc");

        int nameColumn = cursor.getColumnIndex("person");
        int phoneNumberColumn = cursor.getColumnIndex("address");
        int smsbodyColumn = cursor.getColumnIndex("body");
        int dateColumn = cursor.getColumnIndex("date");
        int typeColumn = cursor.getColumnIndex("type");
        if (cursor != null) {
            int i = 0;
            while (cursor.moveToNext() && i++ < 20) {
                SmsInfo smsInfo = new SmsInfo();
                smsInfo.setName(cursor.getString(nameColumn));
                smsInfo.setDate(dateFromLongToString(cursor.getString(dateColumn)));
                smsInfo.setPhoneNumber(cursor.getString(phoneNumberColumn));
                smsInfo.setSmsbody(cursor.getString(smsbodyColumn));
                smsInfo.setType(cursor.getString(typeColumn));
                String personName = getPeople2(smsInfo.getPhoneNumber());
                smsInfo.setName(null == personName ? smsInfo.getPhoneNumber()
                        : personName);
                infos.add(smsInfo);
            }
            cursor.close();
        }
        return infos;
    }
Adi
  • 400
  • 8
  • 25

4 Answers4

1

Basically transnational messages address contains the some pattern. For eg.

AM-HDFCBK

So seeing that , i have made regular expression to fetch that pattern related messages.

Pattern regEx = Pattern.compile("[a-zA-Z0-9]{2}-[a-zA-Z0-9]{6}");

protected BroadcastReceiver myReceiver = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) {
        final Bundle bundle = intent.getExtras();
        try {
            if (bundle != null) {
                final Object[] pdusObj = (Object[]) bundle.get("pdus");
                for (int i = 0; i < pdusObj.length; i++) {

                    SmsMessage currentMessage;
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        String format = bundle.getString("format");
                        currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i], format);
                        Log.e("Current Message", format + " : " + currentMessage.getDisplayOriginatingAddress());
                    } else {
                        currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    }
                    Pattern regEx =
                            Pattern.compile("[a-zA-Z0-9]{2}-[a-zA-Z0-9]{6}");
                    Matcher m = regEx.matcher(currentMessage.getDisplayOriginatingAddress());
                    if (m.find()) {
                        try {
                            String phoneNumber = m.group(0);
                            Long date = currentMessage.getTimestampMillis();
                            String message = currentMessage.getDisplayMessageBody();
                            Log.e("SmsReceiver Mine", "senderNum: " + phoneNumber + "; message: " + message);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        Log.e("Mismatch", "Mismatch value");
                    }
                }
            }
        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" + e);
        }

    }
};

So after that you can check that message body contains the word like credited , debited you can access it.

Piyush
  • 18,895
  • 5
  • 32
  • 63
  • Can U plz tell me how to use ur code with this sample ... http://pastebin.com/YYxktV7D – Adi Oct 15 '16 at 07:47
  • This is the receiver which will fetch your transnational messages from inbox – Piyush Oct 17 '16 at 04:35
  • Ok let me try.. I'm new in this field so need some help. – Adi Oct 17 '16 at 04:37
  • Plz see the updated part of code in question.when I'm trying to put the logic of ur code it's giving nullpointer exception because it's using cursor class for fetching the data and ur code using intents.can U resolve this isssue. @Piyush – Adi Oct 18 '16 at 06:13
  • this regex not only fetch transactions sms but also other messages which are not from banks(not presonal msgs). – Rahul Baghaniya Oct 15 '21 at 07:11
0

For a quick idea, I would like to suggest you few approaches :

First of all it will be quite challenging to sort any transaction types message from inbox, all you can do is either go through each message and read the body and find out your required messages list but that too wont be feasible.

For instance you have to access address field and do the needful as sms possess all fields such as : address, body, received date and more.

Also as you mentioned you know how to read messages from inbox I am skipping that part. Attaching few links which might help you ref this library ,one more

Anurag
  • 1,162
  • 6
  • 20
0

use this for reading transaction messages:

private void readMessages(){
    final int textViewID = searchView.getContext().getResources().
            getIdentifier("android:id/search_src_text", null, null);
    final AutoCompleteTextView searchTextView = (AutoCompleteTextView)
            searchView.findViewById(textViewID);
    try {
        Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
        mCursorDrawableRes.setAccessible(true);
        mCursorDrawableRes.set(searchTextView, 0); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
    } catch (Exception e) {}


    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    String dateVal = "";
    Cursor cursor = this.getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
    if (cursor.moveToFirst()) { // must check the result to prevent exception
        do {
            String msgData = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString();
            String date = cursor.getString(cursor.getColumnIndexOrThrow("date")).toString();
            Long dateV = Long.parseLong(date);
            int start = 0;
            int end = 0;
            String msg = "";
            String add = cursor.getString(2);
            dateVal = formatter.format(new Date(dateV));

            if(!(spam.contains(add) || promo.contains(add))) {
                if(msgData.contains("credited")|| msgData.contains("debited") || msgData.contains("withdrawn")) {
                    messages.add(dateVal + ":" + msgData + "axqw" + add);
                    contentMessage.add(msgData);
                }
            }

        } while (cursor.moveToNext());
    } else {
        // empty box, no SMS
    }
}
AshuKingSharma
  • 757
  • 7
  • 20
0

Every transactional message has the following traits:

  1. Sender is always of the format XX-XXXX
  2. The message will have a/c, account no: in it.
  3. There will be keywords like avbl bal, available balance, balance, combined balance, avl bal.
  4. There will be transactional keywords like debited, credited, payment.

If you can conjugate all these conditions then you will find a transactional message.

Here's a lib that might be of some use https://github.com/minimal-scouser/trny