How can i make an app pop up when i ceartain sms is send to your phone?

2457 views java
5

Here is my code

public void refreshSmsInbox() {

    ContentResolver contentResolver = getContentResolver();
    Cursor smsInboxCursor = contentResolver.query(Uri.parse("content://sms/inbox"), null, null, null, null);
    int indexBody = smsInboxCursor.getColumnIndex("body");
    int indexAddress = smsInboxCursor.getColumnIndex("address");
    if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return;
    arrayAdapter.clear();
        do {
            if (smsInboxCursor.getString(indexAddress).toString()=="6641234567") {

                String str = "SMS From:" + smsInboxCursor.getString(indexAddress) +
                        "\n" + smsInboxCursor.getString(indexBody) + "\n";
                arrayAdapter.add(str);
            }
            else {
                String str = "It doesnt work";
                arrayAdapter.add(str);
            }
            Log.d("Phone Number",smsInboxCursor.getString(indexAddress));
        } while (smsInboxCursor.moveToNext());

}

I want to make a app that pop up when a certain phone number send you a sms I already make that the app read the sms but when i try to make the app show the messages from only certain number it doesnt enter but i dont know why i make a debug resaearh from smsInboxCursor.getString(indexAddress).toString() and it give me this and the result is the same from my condition

also here is the view from my aplication

And here is the view when i removed the if statement

it is very weird for my There is an explination for this? because i can see that the comparasion i made in the if statement is the same that the output in the logcat

answered question

2 Answers

6

Compare string values using equals and not ==.

Change smsInboxCursor.getString(indexAddress).toString()=="6641234567" to smsInboxCursor.getString(indexAddress).equals("6641234567"), then it should register that it works.

posted this
7

To compare strings you need to use equals, == will not work.

Also make sure you are writing the exact text you need to compare to, because a space is also considered a character and can make the comparison not work.

So, change ...toString()=="6641234567" to ...toString().equals("6641234567")

posted this

Have an answer?

JD

Please login first before posting an answer.