Skip to content Skip to sidebar Skip to footer

0 Processes And 1 Service Under Settings, Apps And Running

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 th

Solution 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.

Solution 2:

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:

@OverridepublicvoidonTaskRemoved(Intent rootIntent) {
    IntentrestartService=newIntent(getApplicationContext(), this.getClass());
    restartService.setPackage(getPackageName());
    PendingIntentrestartServicePI= PendingIntent.getService(
        getApplicationContext(), 1, restartService,
        PendingIntent.FLAG_ONE_SHOT);
    AlarmManageralarmService= (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() +1000, restartServicePI);
}

Solution 3:

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,

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

IN Main Activty start alarm manger in this way,

Stringalarm= Context.ALARM_SERVICE;
        AlarmManageram= (AlarmManager) getSystemService(alarm);

        Intentintent=newIntent("REFRESH_THIS");
        PendingIntentpi= PendingIntent.getBroadcast(this, 123456789, intent, 0);

        inttype= AlarmManager.RTC_WAKEUP;
        longinterval=1000 * 50;

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

this will call reciver and reciver is,

publicclassAlarmReceiverextendsBroadcastReceiver {
    Context context;

    @Override
    publicvoidonReceive(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);
        }
    }

    publicstatic 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) {
                    returntrue;
                }
            }
        }
        returnfalse;
    }
}

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

publicclassBackgroundServiceextendsService {
    privateStringLOG_TAG=null;

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

    @OverridepublicintonStartCommand(Intent intent, int flags, int startId) {
        Log.i(LOG_TAG, "In onStartCommand");
        //ur actual codereturn START_STICKY;
    }

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

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

    @OverridepublicvoidonDestroy() {
        super.onDestroy();
        Log.i(LOG_TAG, "In onDestroyed");
    }
}

Post a Comment for "0 Processes And 1 Service Under Settings, Apps And Running"