2

If I start a service with startService in a Activity I get:

1 processes and 1 service

If I now swipe that Activity away. I.e remove it, I get:

0 processes and 1 service

Why is this? And what is a Process and what is a Service in the Android world?

I use START_STICKY and if I stop the service via Settings, Apps and Running, it is not started again, why?

Update1 some code:

Activity:
startService(new Intent(getApplicationContext(), MyService.class));

Service:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "Starting service");


    return(START_STICKY);
}
powder366
  • 4,351
  • 7
  • 47
  • 79
  • Mind posting some code as to what exactly you are doing? – zgc7009 Mar 13 '14 at 14:46
  • @zgc7009 What is not clear? – powder366 Mar 13 '14 at 14:48
  • Are you sure its `0 process and 1 service`? I think there must always be a process as long as your service is running. Did you refresh the screen? – waqaslam Mar 13 '14 at 14:52
  • I am trying to figure out what is in your code that would tell you that you have 0 processes when I don't think that is possible without doing some extensive work. – zgc7009 Mar 13 '14 at 14:55
  • @zgc7009 Swipe away i.e kill/remove it in the task list. As I understand START_STICKY will start the service again but with a null Intent object. Im not sure what you mean with 0 processes. You changed your comment:-) – powder366 Mar 13 '14 at 14:56
  • @waqaslam Yes I refreshed the screen. – powder366 Mar 13 '14 at 15:00
  • @powder366 Yea my first one came off a bit rude, so I thought I could reword it to let you understand I am just trying to figure out why it is doing what it is doing, didn't mean to leave you hanging :P – zgc7009 Mar 13 '14 at 15:01
  • Honestly not sure about it on this end. Like I say, I didn't realize you could even have a service still "active" if the process for that application was killed. Apparently I am wrong, and here is a similar posted question: http://stackoverflow.com/questions/20592366/the-process-of-the-service-is-killed-after-the-application-is-removed-from-the-a – zgc7009 Mar 13 '14 at 15:08
  • @zgc7009 So maybe the 0 process mean that Activity got killed and I have no bound Activity to my Service. Maybe then I have to programatically bind it again? Or if I don't bind will my Activity broadcast still be received in my Service without the bind. – powder366 Mar 13 '14 at 15:18
  • What exactly this screen is showing is a bit unclear, but obviously either the "process" count is not of actual operating system processes, or else "running" doesn't actually mean "has a process to run in" but rather something like "is eligible to run if and when Android feels like granting it a process to run in" – Chris Stratton Mar 13 '14 at 15:37
  • @Chris Stratton Thanks for your answer, but could you write in easier english, Im not sure I understand you... – powder366 Mar 13 '14 at 15:45

3 Answers3

1

what is the definition of process in android world? same as defined at any operating system - your application is "alive" from the system's point of view, it has active memory allocation stack, and may run or not Activities, Services and so on...

I think that you struggling your had "how can it be that running process = 0" but services = 1 not making scenes, and you are right.

the running applications display shown from the settings app is not made only for developers, but also for users, I guess that's why most vendors decided to show active tasks as process. basically, in this display - running process = running task.

most application starts only one task (the main activity with the launcher flag starts automatically in that mode). there will be more tasks only if other activities would start explicitly with that flag.

so, if your app have 2 activities that started at new task mode - you'll see "2 process".

if your app not running at all (your process really not alive) - then you won't see the app in the running apps screen.

Tal Kanel
  • 10,475
  • 10
  • 60
  • 98
  • A bit more on this, a process is the running application itself provided the application is allocated some space on the stack. Services are pieces of the application that are running in the background of that process. – zgc7009 Mar 13 '14 at 15:14
  • So after the Activity task is gone, should I bind to the service manually or should I issue a new startService, once the app launches again? And second part of my question, why is my Service not restarted after manually stopping it (after I removed the Activity from my task list I then stopped the Service)? – powder366 Mar 13 '14 at 15:27
1

Turned out to be a bug in KitKat. (Sometimes I think getting anything done in Android is a big hassle!)

Android Services: START_STICKY does not work on Kitkat

https://code.google.com/p/android/issues/detail?id=63793

Fix in Service:

@Override
public void onTaskRemoved(Intent rootIntent) {
    Intent restartService = new Intent(getApplicationContext(), this.getClass());
    restartService.setPackage(getPackageName());
    PendingIntent restartServicePI = PendingIntent.getService(
        getApplicationContext(), 1, restartService,
        PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() +1000, restartServicePI);
}
Community
  • 1
  • 1
powder366
  • 4,351
  • 7
  • 47
  • 79
  • what does this code do ? i m trying to show notification after every hour from my service class sometimes i m getting notification but clicking on notification is crashing my app and after that it stops my service as well is this bacause my app is getting cleared from recent app list ? thats notification is working but clicking on notification is opening app but crashing app – Erum Jan 18 '15 at 11:13
0

The Main problem in your case is ur unable to start the service when app closed,that time android OS will kill the service, If you are not able to restart the service then call a alam manger to start the reciver like this,

Manifest is,

         <service
            android:name=".BackgroundService"
            android:description="@string/app_name"
            android:enabled="true"
            android:label="Notification" />
        <receiver android:name="AlarmReceiver">
            <intent-filter>
                <action android:name="REFRESH_THIS" />
            </intent-filter>
        </receiver>

IN Main Activty start alarm manger in this way,

String alarm = Context.ALARM_SERVICE;
        AlarmManager am = (AlarmManager) getSystemService(alarm);

        Intent intent = new Intent("REFRESH_THIS");
        PendingIntent pi = PendingIntent.getBroadcast(this, 123456789, intent, 0);

        int type = AlarmManager.RTC_WAKEUP;
        long interval = 1000 * 50;

        am.setInexactRepeating(type, System.currentTimeMillis(), interval, pi);

this will call reciver and reciver is,

public class AlarmReceiver extends BroadcastReceiver {
    Context context;

    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;

        System.out.println("Alarma Reciver Called");

        if (isMyServiceRunning(this.context, BackgroundService.class)) {
            System.out.println("alredy running no need to start again");
        } else {
            Intent background = new Intent(context, BackgroundService.class);
            context.startService(background);
        }
    }

    public static boolean isMyServiceRunning(Context context, Class<?> serviceClass) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);

        if (services != null) {
            for (int i = 0; i < services.size(); i++) {
                if ((serviceClass.getName()).equals(services.get(i).service.getClassName()) && services.get(i).pid != 0) {
                    return true;
                }
            }
        }
        return false;
    }
}

And this Alaram reciver calls once when android app is opened and when app is closed.SO the service is like this,

public class BackgroundService extends Service {
    private String LOG_TAG = null;

    @Override
    public void onCreate() {
        super.onCreate();
        LOG_TAG = "app_name";
        Log.i(LOG_TAG, "service created");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(LOG_TAG, "In onStartCommand");
        //ur actual code
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // Wont be called as service is not bound
        Log.i(LOG_TAG, "In onBind");
        return null;
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        Log.i(LOG_TAG, "In onTaskRemoved");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(LOG_TAG, "In onDestroyed");
    }
}
Ajith K P
  • 398
  • 3
  • 12