Help Simple Sms Project. Android Studio.

Status
Not open for further replies.

aa1012

Fanatic
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!
 
Status
Not open for further replies.

About this Thread

  • 1
    Replies
  • 353
    Views
  • 1
    Participants
Last reply from:
aa1012

Online now

Members online
1,115
Guests online
947
Total visitors
2,062

Forum statistics

Threads
2,277,021
Posts
28,973,765
Members
1,229,687
Latest member
gsby1440xd
Back
Top