I am working on a SMS chat app. I am wondering how to add the latest SMS(sent or receive) at the bottom of the listview when the activity is opened just like stock SMS app or any other messenger.
I tried using ContentObserver. As soon as there is some changes in SMS database, i tried reloading the ListView with the new Dataset but it is not working. Everytime to see a new SMS(sent of receive) i have to restart the app so the list could be reloaded. I want these new message to be displayed in chat window as soon as i sent or receive them just like the way we chat over gtalk, msn or any SMS app.
Here is my code:-
public class ThreadData extends ListActivity
{
private static final Uri SMS_URI = Uri.parse("content://sms");
HashMap<Long, BodyType> bodies = new HashMap<Long, BodyType>();
private String name, number;
private ListView listView;
private Activity activity;
ThreadDataAdapter adapter;
Handler handler;
ArrayList<BodyType> items;
private Context context;
private static final String PHONE_NUMBER_SEPARATORS = " ()-./";
static HashMap<String, ContactInfo.ContactDetail> info = new HashMap<String, ContactInfo.ContactDetail>();
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.chats);
Bundle extras = getIntent().getExtras();
activity = this;
context = this;
listView = getListView();
if(extras != null)
{
number = extras.getString("address");
ContactInfo.ContactDetail nameInfo = getContactsDetailWithNumber(number);
if(nameInfo != null)
{
name = nameInfo.name;
}
else
{
name = number;
}
}
TextView person = (TextView) findViewById(R.id.chat_person);
person.setText(name);
ImageButton callPerson = (ImageButton) findViewById(R.id.call_person);
callPerson.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
popUpDialerAlert();
}
});
buildMessageList();
items = sortBodies(bodies);
adapter = new ThreadDataAdapter(this, items);
listView.setAdapter(adapter);
listView.setStackFromBottom(true);
getContentResolver().registerContentObserver(SMS_URI, true, new MyContentObserver(handler));
//Intent intent = new Intent(this, RBSMSService.class);
//startService(intent);
}
public void loadListView()
{
items.clear();
items = sortBodies(bodies);
adapter = new ThreadDataAdapter(this, items);
listView.setAdapter(adapter);
listView.setStackFromBottom(true);
}
public void buildMessageList()
{
Cursor cursor = getContentResolver().query(SMS_URI, null, null, null, "date ASC");
startManagingCursor(cursor);
while (cursor.moveToNext())
{
BodyType bodyInfo = new BodyType();
String address = cursor.getString(cursor.getColumnIndex("address"));
bodyInfo.body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
Long date = cursor.getLong(cursor.getColumnIndexOrThrow("date"));
bodyInfo.date = date;
String type = cursor.getString(cursor.getColumnIndexOrThrow("type"));
if(type.equals("1"))
{
bodyInfo.type = "received";
}
else if(type.equals("2"))
{
bodyInfo.type = "sent";
}
else if(type.equals("3"))
{
bodyInfo.type = "draft";
}
String number = filterPhoneNumber(address);
ContactInfo.ContactDetail nameInfo = getContactsDetailWithNumber(number);
String personName = number;
if(nameInfo != null)
{
personName = nameInfo.name;
}
else
{
personName = number;
}
if(personName.equals(name))
{
bodies.put(date, bodyInfo);
}
}
}
public void popUpDialerAlert()
{
AlertDialog.Builder dialerAlert = new AlertDialog.Builder(this);
dialerAlert.setTitle("Confirm");
dialerAlert.setMessage("Call" + " " + name + "?");
dialerAlert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
Uri uri = Uri.parse("tel:" + number);
Intent intent = new Intent((Intent.ACTION_CALL));
intent.setData(uri);
context.startActivity(intent);
}
});
dialerAlert.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
}
});
dialerAlert.show();
}
public ArrayList<BodyType> sortBodies(HashMap<Long, BodyType> bodies)
{
ArrayList<Long> dates = new ArrayList<Long>(bodies.keySet());
Collections.sort(dates);
ArrayList<BodyType> items = new ArrayList<BodyType>();
for(Long date : dates)
{
for(Long key : bodies.keySet())
{
if(date.equals(key))
{
items.add(bodies.get(key));
}
}
}
return items;
}
public void printBodies(ArrayList<BodyType> items)
{
for(BodyType bodyType : items)
{
log(bodyType.date.toString());
log(bodyType.body);
log(bodyType.type);
}
}
static class BodyType
{
public String type;
public String body;
public Long date;
}
public static ContactInfo.ContactDetail getContactsDetailWithNumber(String number)
{
if(info.containsKey(number))
{
return info.get(number);
}
return null;
}
public static String filterPhoneNumber(String phoneNumber)
{
if (phoneNumber == null)
{
return null;
}
int length = phoneNumber.length();
StringBuilder builder = new StringBuilder(length);
for (int i = 0; i < length; i++)
{
char character = phoneNumber.charAt(i);
if (PHONE_NUMBER_SEPARATORS.indexOf(character) == -1)
{
builder.append(character);
}
}
return builder.toString();
}
public class MyContentObserver extends ContentObserver
{
public MyContentObserver(Handler handler)
{
super(handler);
}
@Override
public void onChange(boolean selfChange)
{
bodies.clear();
buildMessageList();
items = sortBodies(bodies);
runOnUiThread(new Runnable() {
public void run() {
loadListView();
}
});
super.onChange(selfChange);
}
}
public void log(String msg)
{
Log.e("ThreadData", msg);
}
}
Please Help!
Thanks
You need to keep a reference to your list (items).
When the content provider calls OnChange, clear the very same list (don’t create a new object, recycle your list) and add again your new mails. As this list is tied to your adapter, then it will update your listview when you call notifyDataChanged.
You must recycle the same list.
Regards
Stéphane
PS : please note that you could have kept your old thread to ask the same question again.