Android : Android,Sms,BroadcastReceiver

on Saturday, August 16, 2014


I want to update total messages i.e. count and date of last sms and last sms when new sms received. But its not happening, its happened only when I relaunch activity.



getAll.clear();
Uri SMS_INBOX = Uri.parse("content://mms-sms/conversations?simple=true");

c = getContentResolver().query(SMS_INBOX, null, null, null, "date DESC");

startManagingCursor(c);
count = new String[c.getCount()];
snippet = new String[c.getCount()];
thread_id = new String[c.getCount()];

String[] num=new String[c.getCount()];
date=new String[c.getCount()];
address=new String[c.getCount()];
c.moveToFirst();
for (int i = 0; i < c.getCount(); i++)
{
count[i] = c.getString(c.getColumnIndexOrThrow("message_count"))
.toString();
thread_id[i] = c.getString(c.getColumnIndexOrThrow("_id"))
.toString();
snippet[i] = c.getString(c.getColumnIndexOrThrow("snippet"))
.toString();
date[i]=c.getString(c.getColumnIndexOrThrow("date"))
.toString();

cur=getContentResolver().query(Uri.parse("content://sms/"), null, "thread_id = " + thread_id[i], null, null);
startManagingCursor(cur);
cur.moveToFirst();
num[i]=getContactName(this,cur.getString(cur.getColumnIndexOrThrow("address")).toString());
address[i]=cur.getString(cur.getColumnIndexOrThrow("address")).toString();
getAll.add(count[i]+","+num[i]+","+snippet[i]);
cur.moveToNext();

c.moveToNext();

}
adapter = new HomeAdapter(this, R.layout.sms, getAll,date);

adapter.notifyDataSetChanged();
list.setAdapter(adapter);


BroadCast Reciever



if (null != bndl)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bndl.get("pdus");

msg = new SmsMessage[pdus.length];

for (int i=0; i<msg.length; i++){
msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
contentTitle=msg[i].getOriginatingAddress().toString();
//contentTitle = (String) DateFormat.format("dd/MM/yy:hh:mm a", new Date(msg[i].getTimestampMillis()));
contentText = msg[i].getMessageBody().toString();

}
}


I tried all methods but failed to update and don't know what to do now. I been stuck on this problem from 6 hours.


0 comments:

Post a Comment