What's new

Help Simple Sms Project. Android Studio.

Status
Not open for further replies.

aa1012

Honorary Poster
Joined
Dec 13, 2016
Posts
360
Reaction
113
Points
144
I am currently developing a simple sms app. Im in the stage of handling the received sms and I want to update the sms thread/sms list(on a recyclerview) if the received sms address(I supposed to compare thread_id and not address) is the same as the current opened/viewing thread then I'd like to add the body/sms to the sms thread/sms list. I was able to simply handle the received sms but I can't find a way to add that sms to the sms list/sms thread that is currently opened by the user.
I tried to use an Interface and set the listener to the sms list/sms thread activity but the Interface variable is always null whenever the onReceive is invoked. I found out that everytime an sms is being received, the class that extends the broadcast receiver reinstantiate every single members thus, the interface typed variable will always stays null.
How am I able to address and solve it? I am just a month started programming in Android so I am not yet that familiar with Android SDK.
Here is my Receiver snippet :

Java:
SmsMessage[] smsMessages;
String numAddress, dispAddress, body, dispBody, date_sent;

SmsInterface listener;

public void SetOnReceiveSmsListener(SmsInterface listener) {

    this.listener = listener;
}

@Override
public void onReceive(final Context context, Intent intent) {

    if (intent.getAction().equals(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)) {

        smsMessages = Telephony.Sms.Intents.getMessagesFromIntent(intent);

        for (SmsMessage message : smsMessages) {

            numAddress = message.getOriginatingAddress();
            dispAddress = message.getDisplayOriginatingAddress();
            body = message.getMessageBody();
            dispBody = message.getDisplayMessageBody();
        }

        if (listener != null) {

           listener.onReceive(numAddress, body);
        }
    }
}

The code above supposed to be working on a normal class-interface implementation but on a BroadcastReceiver, it doesnt work.

Sorry for the code snippet. It's just an in-line code. The code tag doesnt seem to work.

Thanks for your help!
 
Last edited:
Status
Not open for further replies.

Similar threads

Back
Top