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 in main Activity:
String body="";
ArrayAdapter<SMSListModel> adapter;
List<SMSListModel> list = new ArrayList<SMSListModel>();
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 for SMSListModel
public SMSListModel(String address, String body) {
this.address = address;
this.body = body;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
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();
}
Upvotes: 0
Views: 129
Reputation:
few code Still zmissing friend but well we can fix code which you post and give you direction for existing one and which you did't post here..
First modify Code for SMSListModel
public class SMSListModel {
String date;
String time;
String address;
String body;
public SMSListModel(String address, String body, String time, String date) {
this.address = address;
this.body = body;
this.time = time;
this.date = date;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
Change update 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"))),cursor.getColumnIndex("time")) , cursor.getColumnIndex("date")) );
}
}
}
return list;
}
NOTE : For cursor.getColumnIndex("time")) and cursor.getColumnIndex("date")) first you need to create two column with name time,date in your database class and also insert value for that when sms receive.
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();
date =list.get(i).getDate();
time =list.get(i).getTime();
else
body =list.get(i).getBody();
date =list.get(i).getDate();
time =list.get(i).getTime();
try
{
String mbody = "from"+ date + time +body;
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, mbody, null, null);
}
catch (Exception e)
{
//Error Occurred if No Messages Selected
e.printStackTrace();
}
**Note: You need to modify your database. for that modify table and add two column at end "time" and "date".
after that you also need to change your broadcastReceiver subclass's code which receive message and store this message into database. from that class you also need to enter time and date value to your table.**
Upvotes: 1