0

I am implementing an app kind of an app tracker which requires to run the service all the time in background. So the service is called when application is opened and it stops when stopself() is called. Service also consists of a thread which runs all the time. It used to run perfect. But from last few days it stops after sometime. When coming to my app's ui after some task the service stops! Can anyone suggest me any solution?

   public int onStartCommand(Intent intent, int flags, int startId) {

        Log.e("SERVICE","started");
        if(intent != null && intent.getAction() != null) 
        {
            day=intent.getExtras().getInt("day")+5;
            Toast.makeText(this, "day" +day, Toast.LENGTH_LONG).show();
            apps=intent.getStringArrayListExtra("apps");
            items=apps.size();
            timer=intent.getIntegerArrayListExtra("timer");
            Run[] a=new Run[items];
            t=new Thread[items];

            for(int i=0;i<items;i++)
            {
               int sec=0;
               app=apps.get(i).toString();
               time=timer.get(i);
               a[i]=new Run(app, time,sec);
               Log.e("APPTIME",app+ " "+time);
               t[i]=new Thread(a[i]);
               t[i].start();
            }

        }

    return super.onStartCommand(intent, flags, startId);
    }

2 Answers2

1

You should start your service as STICKY_SERVICE

Example for starting as STICKY

Thread for START_STICKY and START_NOT_STICKY

You should consider reading this, if you are working on Android KitKat

Community
  • 1
  • 1
VenomVendor
  • 15,064
  • 13
  • 65
  • 96
  • When i used start sticky it gives me null intent! And since i am checking this condition if(intent != null && intent.getAction() != null) the service isn't restarted. – user3855998 Aug 18 '14 at 11:12
0

Try implementing foreground service. foreground service

Foreground service displays notification and is never stopped until you want.

Implement this code snippet in your service's onCreate

Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
        System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
        getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);
Adhikari Bishwash
  • 2,770
  • 28
  • 32
  • Thank you so much for this suggestion. But is there any other way because my app shows notification when its task gets complete. – user3855998 Aug 18 '14 at 11:29
  • yo can dismiss notification as your tasks get completed. If you dont want your service to get stopped in any condition, this is the way. – Adhikari Bishwash Aug 18 '14 at 11:34
  • well it shows notification to ask the user whether to continue the task when a day is completed ! so in this case what can be done? – user3855998 Aug 18 '14 at 11:36
  • you just show notification like "MYApp service is running" or any message. – Adhikari Bishwash Aug 18 '14 at 11:41
  • Ohk so throughout its running process the notification is present. But then how to alert the user to input details when the day gets complete with the same notification? From the user's point of view it might be the notification of "Running service". – user3855998 Aug 18 '14 at 11:48
  • ya .. yo got it. Notification you get when music player is playing music which means service needs to run always. – Adhikari Bishwash Aug 18 '14 at 11:51
  • But then how to alert the user to input details when the day gets complete with the same notification? – user3855998 Aug 18 '14 at 11:54
  • you can start your activity to get inputs from user when needed. – Adhikari Bishwash Aug 18 '14 at 12:07