Reputation: 245
I'm creating this post to get help. I am developing an application which sends incoming text sms
.What I'm doing is fetching incoming message body, date and time
and send it as new message. for sending purpose I'm using sms manager
. I'm able to get multiple message body using checkboxes
and creating a list
of selected messages. But the problem is in getting their date and time.
Code for array list of selected messages:
private List<SMSListModel> getModel()
{
if(cursor.getCount()>0)
{
for(int i=0;i<cursor.getCount();i++)
{
if(cursor.moveToPosition(i))
{
list.add(new SMSListModel(cursor.getString(cursor.getColumnIndex("address")),cursor.getString(cursor.getColumnIndex("body"))));
}
}
}
return list;
}
Code to send selected message body:
if(list.size()>0){
for(int i=0;i<list.size();i++)
{
if(list.get(i).isSelected())
{
if(body.equals(""))
body =list.get(i).getBody();
else
body =list.get(i).getBody();
try
{
String mbody = "from"+ "dd/mm/yy" +"hh:mm"+body;
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, mbody, null, null);
}
catch (Exception e)
{
//Error Occurred if No Messages Selected
e.printStackTrace();
}
Look at once and please provide me with proper edits if possible
Upvotes: 0
Views: 253
Reputation: 47817
Hey try like this way:
ArrayList<Object> time = new ArrayList<Object>();
Uri uri = Uri.parse("content://sms/inbox");
c = getContentResolver().query(uri, null, null, null, null);
startManagingCursor(c);
// Read the sms data and store it in the list
if (c.getCount() > 0) {
if (c.moveToFirst()) {
for (int i = 0; i < c.getCount(); i++) {
String date = c.getString(c.getColumnIndex("date"));
Long timestamp = Long.parseLong(date);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp);
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String messageDate = formatter.format(calendar.getTime());
time.add(messageDate);
c.moveToNext();
}
}
Update: And also change Date format
like:
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String strBody="from "+time.get(position)+body;
Upvotes: 1
Reputation: 840
You need to define another member inside your model. here's how you can read date and set it to your model via constructor/setter.
private List<SMSListModel> getModel()
{
if(cursor.getCount()>0)
{
for(int i=0;i<cursor.getCount();i++)
{
if(cursor.moveToPosition(i))
{
SMSListModel model=new SMSListModel(cursor.getString(cursor.getColumnIndex("address")),cursor.getString(cursor.getColumnIndex("body")));
String date = cursor.getString(cursor.getColumnIndex("date"));
Long timestamp = Long.parseLong(date);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp);
Date finaldate = calendar.getTime();
String smsDate = finaldate.toString();
model.setSMSDate(smsDate);
list.add(model);
}
}
}
return list;
}
Inside your send SMS code logic, just read it from the model and add it to your SMS body
list.get(i).getSMSDate();
haven't tested this but you get the point.
Upvotes: 0