Update ListView every second with Handler not working

170 Views Asked by At

In my application where the app receives a new sms message, I want the listview to update it it immediately with the new message shown in the listview. I tried using the example code of the broadcast receiver code but it didn't work, so I'm now trying to use a timer to update the listview every second and show the latest message received but this isn't working either.

 final Handler handler = new Handler();
                handler.postDelayed(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        inboxArrayAdapter.notifyDataSetChanged();

                        ((BaseAdapter)inboxmessages.getAdapter()).notifyDataSetChanged();

                        handler.postDelayed(this,  1000 );

                    }
                }, 1000);

        return view;

Original attempt of using BroadcastReceiver to update listview when new SMS message recieved.

SmSBroadcastReceiver.java

public class SmsBroadcastReceiver extends BroadcastReceiver
{
    public static final String SMS_BUNDLE = "pdus";

    // https://javapapers.com/android/android-receive-sms-tutorial/
    public void onReceive(Context context, Intent intent)
    {
        Bundle intentExtras = intent.getExtras();
        if (intentExtras != null)
        {
            Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
            String smsMessageStr = "";

            for (int i = 0; i < sms.length; i++)
            {
                // Get sms message
                SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);

                // Get content and number
                String smsBody = smsMessage.getMessageBody().toString();
                String address = smsMessage.getOriginatingAddress();

                // Create display message
                smsMessageStr += "SMS From: " + address + "\n";
                smsMessageStr += smsBody + "\n";

                // Add it to the list
                CommandsFragment inst = CommandsFragment.instance();
                inst.updateList(smsMessageStr);
            }
            CommandsFragment inst = CommandsFragment.instance();
            inst.updateList(smsMessageStr);
        }
    }
}

CommandsFragment.java

 public static CommandsFragment instance()
        {
            return inst;
        }


        @Override
        public void onStart() {
            super.onStart();
            inst = this;
        }


        public static CommandsFragment newInstance()
        {
            CommandsFragment fragment = new CommandsFragment();
            return fragment;
        }

 public void updateList(final String smsMessage)
    {
        inboxArrayAdapter.insert(smsMessage, 0);
        inboxArrayAdapter.notifyDataSetChanged();
    }
2

There are 2 best solutions below

5
vss On BEST ANSWER

Put the listview update code inside a method in the Activity where the listview belongs, and call that method from broadcast receiver's onReceive. It would definitely work. Also check the code for whether you are updating the list view properly.

public class SmsBroadcastReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context paramContext, Intent intent) { // change the parameters depending upon your requirements

    // do something here with the sms received if necessary

    // then call the listview update method here        
   }

}
0
Sahil Bora On

Also to add to the answer don't forget to have this below in the AndroidManifest.xml file

  </activity>
        <receiver android:name=".SmsBroadcastReceiver">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    </application>

This above also fixed the solution for me. Thanks for your help guys.