Restnom
Restnom

Reputation: 134

How to make messages show up automatically when recieved in Android SMS app?

So I have this pretty simple app. It sends encrypted SMS messages to the specified phone number. It works swell, but I am having issues finding a way to make the recieved messages automatically show up in the message log. I currectly have a "refresh" button that updates the message log if a new message is available. I don't want to have to use a refresh button, I want the message to simply show up as it is received.

The way the app works is, it takes the message to be sent from the textbox and encrypts it. It then sends the message and when received, it decrypts and stores in a variable and presents in the message log (after I press "refresh").

I have tried many searches on google but can not find something useful because the words are too sensetive. I usually find links to people not being able to receive messages or just links to download messaging apps.

Here is some of my code. This is the receive sms part.

public class SmsReceiver extends BroadcastReceiver
{    
    public static String decrypted = "";

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

        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String str ="";
        String info = "";


        if (bundle != null)
        {


            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                info += "SMS from " + msgs[i].getOriginatingAddress();
                info += " : ";
                str += msgs[i].getMessageBody().toString();

                if (i==msgs.length-1 && MainActivity.locked == true){

                    try {
                        decrypted = AESHelper.decrypt(MainActivity.seed, str);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    MainActivity.rand = Math.random();
                    MainActivity.seed = String.valueOf(MainActivity.rand);

                    decrypted = info + decrypted;
                    info = "";
                    MainActivity.locked = false;



        }
    }
}

So, in my main activity, I have the refresh button set to check the length of decrypted. If length of decrypted > 0 then I take decrypted's content and display them in the message log.

Upvotes: 0

Views: 473

Answers (1)

Susheel
Susheel

Reputation: 784

There are multiple ways to do this. Simple way would be to use TimerTask that runs every second in your MainActivity to check the length of 'decrypted' variable. Timertask does not run on ui thread. But since you are using it only to check the length of a variable you should be fine. If you need help with timertask, use my example below:

     Timer timer = new Timer();
     timer.scheduleAtFixedRate(new SpecificTask(), 1000, 200);

But you have to define your SpecificTask() class...

     private Specific Task extends TimerTask{
         @Override
    public void run() {

                if(decrypted.length()>0){
                 //do refresh here but make sure you run this code onuithread
                  //just use runonuithread... 
                  }

            }

       }

Or you could just use a handler object...

      boolean mStopHandler = false;
      Handler handler = new Handler();

     Runnable runnable = new Runnable() {
     @Override
     public void run() {
     if (!mStopHandler) {
        if(decrypted.length()>0){
               //refreshlayout code
        }
        handler.postDelayed(this, 500);
       }
     }
   };

       // start your handler  with:
        handler.post(runnable);

Upvotes: 1

Related Questions