I have two services in my app, One for CountDownTimer and one is for Window manager. My main activity is
public class MainActivity extends ActionBarActivity {
// Variables
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// some code here
}
// Broadcase reciever for Countdown Timer
private BroadcastReceiver br = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateGUI(intent); // or whatever method used to update your GUI
// fields
}
};
private void updateGUI(Intent intent) {
if (intent.getExtras() != null) {
long millisUntilFinished = intent.getLongExtra("countdown", 0);
HeadService.textView.setText("" + millisUntilFinished / 1000);
Log.i("YOLO", "Countdown seconds remaining: " + millisUntilFinished
/ 1000);
}
}
// Registrating and Starting services in onStart()
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
startHeadService();
registerReceiver(br, new IntentFilter(BroadcastService.COUNTDOWN_BR));
Log.i("Check", "Registered broacast receiver");
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onStop() {
try {
unregisterReceiver(br);
stopHeadService();
} catch (Exception e) {
// Receiver was probably already stopped in onPause()
}
super.onStop();
}
@Override
public void onDestroy() {
stopService(new Intent(this, BroadcastService.class));
stopHeadService();
Log.i("Check", "Stopped service");
super.onDestroy();
}
private void startHeadService() {
startService(new Intent(this, HeadService.class));
}
private void stopHeadService() {
stopService(new Intent(this, HeadService.class));
}
}
When ever I change orientation, My services are stopped and new services start again. So basically what I want to know is where to start and stop my services so it keep running even in orientation change. Also i know what activity life cycle is followed on Orientation change from here Which activity method is called when orientation changes occur? and I am just not getting what should I do. Thanks in advance.