1

I have to send sms periodically through my android application to a certain group of people (friends). The messages to be sent will be stored in a database in the application only.

I have seen following threads:

How to send sms programmatically?

Show compose SMS view in Android

Cœur
  • 37,241
  • 25
  • 195
  • 267
Shruti
  • 1
  • 13
  • 55
  • 95

1 Answers1

2

Use a Service:

import java.util.Timer;
import java.util.TimerTask;


import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;

public class SrvSMSSender extends Service {    

    Timer timerSendSMS = new Timer();

    class taskSendSMS extends TimerTask {
        @Override
        public void run() {
            hSendSMS.sendEmptyMessage(0);
        }
    };

    final Handler hSendSMS = new Handler(new Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            procSendSMS();
            return false;
        }
    });

    public void procSendSMS() {
        try {
            // send your SMS here

        } catch (Exception e) {

        }
    }


    @Override
    public void onCreate() {
        super.onCreate();

    };

    @Override
    public void onStart(Intent intent, int startId) {
        try {
            long intervalSendSMS = 10*60*1000;

            timerSendSMS = new Timer();

            timerSendSMS.schedule(new taskSendSMS(), 0, intervalSendSMS);

        } catch (NumberFormatException e) {
            Toast.makeText(this, "error running service: " + e.getMessage(),
                    Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(this, "error running service: " + e.getMessage(),
                    Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onDestroy() {

        timerSendSMS.cancel();
        timerSendSMS.purge();

    }
}
Bob
  • 22,810
  • 38
  • 143
  • 225
  • thanks for response..but tell me can we send messages periodically say after every 24hrs to a specific group – Shruti Dec 28 '11 at 05:53
  • set `long intervalSendSMS = 10*60*1000;` for 24 hours. but i do not know that how you can get a group's phone numbers. – Bob Dec 28 '11 at 05:58