0

I have the code below which is in ONCreate() method of the main activity and works fine. After moving to the next activity (B) which is a list view and then to activity (C ) which is the row selected from the listview and goes to sleep then the ScheduledExecutorService started in the main activity is not executing anymore .

Why is it not executing and how do I fix it? I am noob in Android .Please help me out.

Thanks in Advance .Really appreciate the help.

ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
            executor.scheduleWithFixedDelay(new Runnable(){

                public void run() {


    //play audio file

    mp3.start();

                }

            }, 0, 60, TimeUnit.SECONDS);
James Patrick
  • 273
  • 4
  • 19
  • Use Alarm Manager instead of ScheduledExecutorService – baboo Feb 11 '13 at 14:12
  • but do I have to use service along with it ? – James Patrick Feb 11 '13 at 14:14
  • 1
    it depends on nature of task... check this: http://stackoverflow.com/questions/5766912/android-background-service-and-alarmmanager – baboo Feb 11 '13 at 14:44
  • yes something similar .But will the alarm manager still keep running even if the app in closed or forced to stop from settings ? By the way thanks for your time . – James Patrick Feb 11 '13 at 14:47
  • 1
    np with my time , http://stackoverflow.com/questions/3859489/android-running-a-background-task-using-alarmmanager check this... one friendly suggestion , increase your googling :D – baboo Feb 11 '13 at 14:52
  • 1
    http://stackoverflow.com/questions/12744933/how-to-restart-service-using-intentservice-in-android and http://stackoverflow.com/questions/11527581/how-to-ensure-alarmmanager-survives-phone-restart-and-application-kill – baboo Feb 11 '13 at 14:56
  • so by default the service will stop if the app is killed or force closed right ? this question has been troubling me for a long time . – James Patrick Feb 11 '13 at 15:02
  • I want it to closed and begin again when the user starts the application . – James Patrick Feb 11 '13 at 15:02
  • 1
    if you only want it to start when user starts the application you can start the bind the service on oncreate of your app , and unbind ... do you want the service only to run while the app is running or continue running when app is no longer in foreground ? if you want the app to continue running then use startservice with STICKY flag – baboo Feb 11 '13 at 15:08
  • it should run only till the app is running and stop if app is killed or closed .Thats the current requirement . – James Patrick Feb 11 '13 at 15:14
  • 1
    Then use simple AlarmManager, no need of service http://stackoverflow.com/questions/4643850/android-alarm-is-cancelled-after-closing-the-application – baboo Feb 11 '13 at 15:31
  • Thanks baboo .Will check it out and let you know how it goes.Thanks again for your time. – James Patrick Feb 12 '13 at 02:16
  • Another query I have Baboo Whats the difference between AlarmManager vs ScheduledExecutorService .Or Pros and cons. PLease Help. – James Patrick Feb 12 '13 at 02:44
  • 1
    http://stackoverflow.com/questions/6558694/difference-between-alarmmanager-and-scheduledexecutorservice – baboo Feb 12 '13 at 07:26
  • Hi Baboo thanks for the link.I checked it and noticed that I need to use AlarmManager which works like a ScheduledExecutorService(Sorry for the confusion).In other words ScheduledExecutorService works till app is running and not sleeping ,but it does not manage memory . I want it to run when its not sleeping and only if the app is running in the background . How do I do the same wit Alarm Manager ? I am looking in the youtube tutorial for alarm manager but I did not find it good .Please suggest me something to resolve my query. – James Patrick Feb 12 '13 at 08:38
  • I am currently using ScheduledExecutorService in mu app and would like to implement alarmManage in its place .Thanks again for your time. – James Patrick Feb 12 '13 at 08:40
  • 1
    with the app running and the set sleeping learn to use alarm manager with WAKELOCK... i will search for a tutorial and post it .. but u do the same – baboo Feb 12 '13 at 10:23
  • Thank You Sir.Thats very generous of you. I am doing the same right now. I got a tutorial on streaming music with service .I am checking how service is used but so far I dont think its required in my case. Case1) check server for new messages in background using asynctask with ScheduledExecutorService and case2) update user location in background I am using ScheduledExecutorService and asynctask .I will be replacing AlarmManager in its place. I dont thinh service is required.What do you think ? Thanks for cooperating and leading me through confusion and doubt. – James Patrick Feb 12 '13 at 10:31
  • Please put your answer as answer so that I can accept it.Also I dont have enough points to chat.Thanks Again. – James Patrick Feb 12 '13 at 10:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24501/discussion-between-james-patrick-and-baboo) – James Patrick Feb 14 '13 at 09:52

1 Answers1

1

In order to cater the app running and the set sleeping scenario look in to using alarm manager with WAKELOCK... example : http://code4reference.com/2012/07/tutorial-on-android-alarmmanager/ and Alarm Manager Example

Community
  • 1
  • 1
baboo
  • 1,983
  • 17
  • 23