• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

send scheduling sms through android

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Jazz Cool",

We don't have many rules here at the JavaRanch, but we do insist that you use your Real Name and not some sort of "handle" or obvious alias. If you're not sure about this, see
http://www.javaranch.com/name.jsp .

A lot of people here won't bother to answer a post that has an invalid display name on it or is full of non-English words (includins SMS/733tsp3k", so it's worth your while to take a little time and make things look professional. After all, we have our image to maintain (see our logo? )
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic