2

I want to make a service in android which run in background always and start as soon as I boot my phone and send message at a regular interval.I have writen my code as below

MainActivity.class

 package test.sai;



 public class MainActivity extends Activity {

Timer t;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    alrm();
    Log.e("msg", "in main");


}






public  void alrm() {
    Intent myAlarm = new Intent(getApplicationContext(), AlarmReceiver.class);
    Log.e("msg", "in alrm");
    //myAlarm.putExtra("project_id", project_id); //Put Extra if needed
    PendingIntent recurringAlarm =      v     PendingIntent.getBroadcast(getApplicationContext(), 0, myAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarms = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    Calendar updateTime = Calendar.getInstance();
    Log.e("msg", "in alrm1");
    //updateTime.setWhatever(0);
    alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP,        updateTime.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, recurringAlarm); //you can modify the interval of course

}




}

This class is calling AlarmReceiver.class

package test.sai;



 public class AlarmReceiver extends BroadcastReceiver 
 {  
GPSTracker gps;

@Override
public void onReceive(Context context, Intent intent) 
{
    gps = new GPSTracker(context);

    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {  
           Intent pushIntent = new Intent(context,MainActivity.class); 
           pushIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           context.startActivity(pushIntent);



           Log.e("pro", "alrmmanager");
           }

    Intent myService = new Intent(context, FirstService.class);
    myService.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startService(myService);
    Log.e("msg", "in alrmmanager1");





}

 }  

and finally AlarmReceiver is calling the service class

 package test.sai;



 public class FirstService extends Service{

Timer t;
int time = 0;

@Override
public IBinder onBind(Intent arg0) {


    // TODO Auto-generated method stub
    return null;
}
@Override
public void onStart(Intent intent, int startId) {


    Log.e("time", time++ +"");
    Toast.makeText(this, time+1+"", 500).show();





}
@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();

}

}

Now I want to on GPS as soon as service starts and then I want to use GPS to track location of mobile and send a message to another mobile.I also have code for GPS and sms sending but I am not getting how to call and where to call those methodss,so that my service keep on running and sending messages at some perticular interval.please help.

Sangeeta Rawat
  • 199
  • 1
  • 1
  • 15

3 Answers3

0

You can use alarmManager for this... Because if you create your own timerTask, it is very much susceptible to get destroyed by the processor.

Abhishek Shukla
  • 1,242
  • 8
  • 11
  • You're asking someone else to do all the work. @abhishek-shukla and the other answerers have all pointed you in the right direction, but it's up to you to put the pieces together. No one will write your app for you. – Rahat Ahmed Jul 20 '13 at 10:45
  • @RahatAhmed I have edited the question above,now can u please direct me further. – Sangeeta Rawat Jul 26 '13 at 08:51
0

To answer your two part question:

First you need to learn how to handle onBoot within Android Framework. Refer to this Q/A Trying to start a service on boot on Android

Lastly you need to understand the SMSManager class. Refer to the documentation http://developer.android.com/reference/android/telephony/SmsManager.html

I don't think anyone should provide complete code for your request as your main problem/question is "How can I help myself and stop looking for others to fix all my problems".

Community
  • 1
  • 1
wurde
  • 2,487
  • 2
  • 20
  • 39
0

Try registering a BroadcastReceiver with AlarmManager to receive an intent at your regular interval. You'll probably want two, one that listens for a BOOT_COMPLETED action, and another that the AlarmManager will start on interval. You can have the second receiver start a service if whatever you want to do will take a while to execute.

Here's a question on how to make the receiver run on boot so you can register the other receiver with AlarmManager: Android BroadcastReceiver on startup

Here's another that wants pretty much the same thing you want, minus the SMS: How to Autostart an AlarmManager to start a Scheduled Activity?

Community
  • 1
  • 1
Rahat Ahmed
  • 2,191
  • 2
  • 30
  • 40