i had used the sms manager for sending sms
but bot facing problem if i want to send more than one schedule message
my code is..
public class alaramservice extends Service
{
int prefName,pref_resend;
DatabaseConnectionAPI con;
Cursor curText,curContacts;
String subject,body;
String strcontacts=" ";
String[] contacts;
Context ctx;
setAlarm sa;
SharedPreferences msgid_resend;
Editor prefsEditor_resend;
int[] msg_resendShare;
int flag;
@Override
public void onCreate()
{ // TODO Auto-generated method stub
System.out.println("on create called..");
con = new DatabaseConnectionAPI(alaramservice.this);
ctx=this;
con = new DatabaseConnectionAPI(ctx);
try
{
con.createDataBase();
}
catch (Exception e)
{
System.out.println("The database couldnt be started from service "+ e);
}
msgid_resend = ctx.getSharedPreferences("preference_resend", Context.MODE_WORLD_READABLE);
prefsEditor_resend = msgid_resend.edit();
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
prefName = myPrefs.getInt("sendid", 0);
System.out.println("message id got through shared preferences.."+prefName);
// Toast.makeText(this, "alarmservice.onCreate()", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId)
{ // TODO Auto-generated method stub
super.onStart(intent, startId);
String mid=Integer.toString(prefName);
System.out.println("on start called..and msg id got @@@@@@@@@@@@@@@@@@"+mid);
// for sending failure messages first whenever service called
Cursor sendagain=con.onFireQueryGetCursor("select a1.msg_text,a2.contact_no,a1.msg_id from msg_table a1,master_table a2 where a1.msg_id=a2.msg_id and (a2.remark <> \"result_ok\")");
System.out.println("number of failed messages"+sendagain.getCount());
sendagain.moveToFirst();
for(int i=0;i<sendagain.getCount();i++)
{
prefsEditor_resend.putInt("resendid", sendagain.getInt(2));
prefsEditor_resend.commit();
pref_resend=msgid_resend.getInt("resendid", 0);
System.out.println("@@@ id of message for resending"+pref_resend);
System.out.println("contact...."+sendagain.getString(1));
flag=0;
sendSMS(sendagain.getString(1),sendagain.getString(0));
sendagain.moveToNext();
}
// start service newly arrived messages
curText=con.onQueryGetCursor("msg_table", new String[] {"msg_subject","msg_text"}, "msg_id=?", new String[] {mid}, null, null, null);
curText.moveToFirst();
subject=curText.getString(0);
body=curText.getString(1);
curContacts=con.onQueryGetCursor("master_table", new String[] {"contact_no"}, "msg_id=?", new String[] {mid}, null, null, null);
curContacts.moveToFirst();
System.out.println("number of messages to send..."+ curContacts.getCount());
for(int i=0;i<curContacts.getCount();i++)
{
System.out.println("contact numbers ...."+curContacts.getString(0));
flag=1;
sendSMS(curContacts.getString(0),body);
curContacts.moveToNext();
}
updateData();
Toast.makeText(this, "alarmservice started", Toast.LENGTH_LONG).show();
stopSelf();
}
private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver()
{
@Override
public void onReceive(Context arg0, Intent arg1)
{
String idtochange="";
if(flag==0)
{
System.out.println("for resending idssss");
idtochange=Integer.toString(pref_resend);
}
if(flag==1)
{
idtochange= Integer.toString(prefName);
}
System.out.println("the result code...."+getResultCode());
switch (getResultCode())
{
case Activity.RESULT_OK:
System.out.println("updating from pending to sent....");
Toast.makeText(arg0, "SMS sent",Toast.LENGTH_LONG).show();
con.onUpdateSet("master_table", "trans_status","sent", new String[] {"msg_id"},new String[] {idtochange});
con.onUpdateSet("master_table", "remark", "result_ok", new String[] {"msg_id"},new String[] {idtochange});
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
System.out.println("failed because of some problem...........");
Toast.makeText(arg0, "Generic failure",Toast.LENGTH_LONG).show();
con.onUpdateSet("master_table", "remark", "generic_failure", new String[] {"msg_id"},new String[] {idtochange});
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",Toast.LENGTH_LONG).show();
con.onUpdateSet("master_table", "remark", "no_serivce", new String[] {"msg_id"},new String[] {idtochange});
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",Toast.LENGTH_LONG).show();
con.onUpdateSet("master_table", "remark", "null_pdu", new String[] {"msg_id"},new String[] {idtochange});
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",Toast.LENGTH_SHORT).show();
con.onUpdateSet("master_table", "remark", "radio_off", new String[] {"msg_id"},new String[] {idtochange});
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver()
{
@Override
public void onReceive(Context arg0, Intent arg1)
{
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",Toast.LENGTH_LONG).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
System.out.println("contactsssss"+phoneNumber);
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
public void updateData()
{
System.out.println("helllooo called for sending another message from alarm manager");
sa = new setAlarm(ctx);
sa.setMinMsgID();
}
@Override
public IBinder onBind(Intent intent)
{ // TODO Auto-generated method stub
Toast.makeText(this, "alarmservice.onBind()", Toast.LENGTH_SHORT).show();
return null;
}
@Override
public void onDestroy()
{ // TODO Auto-generated method stub
try
{
con.close();
}
catch (Exception e)
{
System.out.println("The database couldnt be started from service "+ e);
}
super.onDestroy();
Toast.makeText(this, "alarmservice.onDestroy()", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onUnbind(Intent intent)
{ // TODO Auto-generated method stub
Toast.makeText(this, "alarmservice.onUnbind()", Toast.LENGTH_SHORT).show();
return super.onUnbind(intent);
}
}
through update method i use to call once again alarm class for setting next nearest alarm to fire