Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7573059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:05:06+00:00 2026-05-30T16:05:06+00:00

I realized,there’re some questions here, but i think my code is really different from

  • 0

I realized,there’re some questions here, but i think my code is really different from them (i’m a beginner) and i can’t understand their answers!

So i want to refresh my ListView everytime i receive a SMS, i tried to use cursor.requery(); and some methods which i found on google but it’s still not working.

This is my code :

public class SMSActivity extends Activity implements OnItemClickListener {

ArrayList<String> smsList = new ArrayList<String>();
// String ADDRESS[];
// int total = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(Uri.parse("content://sms/inbox"), null, null,
            null, null);

    int indexBody = cursor.getColumnIndex("body");
    int indexAddr = cursor.getColumnIndex("address");

    if (indexBody < 0 || !cursor.moveToFirst())
        return;

    smsList.clear();
    do {
        String str = "Sender : " + cursor.getString(indexAddr) + "\n"
                + cursor.getString(indexBody);
        smsList.add(str);
        // ADDRESS[total] = cursor.getString(indexAddr);
        // total++;
    } while (cursor.moveToNext());

    ListView lvSms = (ListView) findViewById(R.id.SMSList);
    lvSms.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, smsList));

    // cursor.requery();
    lvSms.setOnItemClickListener(this);
}

EDITED :
And this is the Class where i extends it with BroadcastReceiver :

public class SMSReceiver extends BroadcastReceiver {

private static final int NOTIF_ID = 0;

@Override
public void onReceive(Context arg0, Intent arg1) {
    // TODO Auto-generated method stub

    Bundle bundle = arg1.getExtras();
    SmsMessage[] msgs = null;
    String str = "";
    if (bundle != null) {
        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]);
            str += "You Get New SMS from " + msgs[i].getOriginatingAddress();
            str += " :";                    str += msgs[i].getMessageBody().toString();
            str += "\n";

        Toast.makeText(arg0, str, Toast.LENGTH_SHORT).show();

        NotificationManager nm = (NotificationManager) arg0.getSystemService(Context.NOTIFICATION_SERVICE);

        String tickerText = str;

        Notification notif = new Notification(R.drawable.ic_launcher, tickerText, System.currentTimeMillis());

        notif.flags = Notification.FLAG_AUTO_CANCEL;

        String contentTitle = msgs[i].getOriginatingAddress();
        String contentText = msgs[i].getMessageBody().toString();

        Intent intent = new Intent(arg0, SMSReply.class);
        PendingIntent pi = PendingIntent.getActivity(arg0, 0, intent, 0);

        notif.setLatestEventInfo(arg0, contentTitle, contentText, pi);
        notif.defaults = Notification.DEFAULT_ALL;
        nm.notify(NOTIF_ID, notif);

        String tempSMS = msgs[i].getOriginatingAddress();
        Intent pass = new Intent();
        Bundle bundlePass = new Bundle();

        bundlePass.putString("key", tempSMS);
        pass.putExtras(bundlePass);
        }
    }
}

I don’t understand how to link those two Class so everytime the BroadcastReceiver works(new SMS come) my ListView will be updated(just like the SMS app in your phone)

Thanks All.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:05:08+00:00Added an answer on May 30, 2026 at 4:05 pm

    Define adapter as global public variable,

    ArrayAdapter<String> adapter;
    

    inside onCreate() assign it,

    adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, smsList));
    

    in your BroadcastReceiver access the smsList and add to it the new sms,

    then access your adapter this way,

    adapter.notifyDataSetChanged();
    

    this function will notify the adapter that smsList values have been changed and will update the list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While implementing the code from this question on my project I realized there's 3
I realize there are similar questions on this topic, but I still cannot find
I've downloaded log4net, but I've realised that there are different assemblies for .NET and
After doing some changes to my plugin amtyThumb , I realized that there is
I've realized there are a lot of different methods of setting up a truststore
I'm not a beginner, but the more I think about this issue, the more
I realize there is a somewhat related thread on this here: Loading assemblies and
I realize there's no definitely right answer to this question, but when people talk
I realize there's a way in Vim to hide/fold lines, but what I'm looking
I would like to quickly send email from the command line. I realize there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.