need a service that will run in background and it will awake after 30-120 seconds and find the location where the device is,then send the location to the data center and go to sleep.Again 30-120 seconds later service awake and determine location and go to sleep. I can start and stop the service manually.But i need to start(between 30 to 120 sec) and stop the service automatically.I can not remain the service live cause it will drain the battery.
So,My question is how can i start and stop the service automatically?
Thanks advance for any suggestion.
My code is
public class Service extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button start = (Button)findViewById(R.id.startButton);
Button stop = (Button)findViewById(R.id.stopButton);
start.setOnClickListener(startListener);
stop.setOnClickListener(stopListener);
}
private OnClickListener startListener = new OnClickListener() {
public void onClick(View v){
startService(new Intent(Service.this,SimpleService.class));
}
};
private OnClickListener stopListener = new OnClickListener() {
public void onClick(View v){
stopService(new Intent(Service.this,SimpleService.class));
}
};
}
You can user
AlarmManagerClass for your requirement. Create an alarm that will awake aServiceon Specific Timer Interval ( in your case 30-120 seconds ). while awaking destroy the previous running service by calling itonDestroy()method.