Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Non sticky service gets recreated (indefinetly?)
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  15 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Johan Appelgren  
View profile  
 More options Aug 8 2012, 4:52 pm
From: Johan Appelgren <johan.appelg...@gmail.com>
Date: Wed, 8 Aug 2012 13:52:59 -0700 (PDT)
Local: Wed, Aug 8 2012 4:52 pm
Subject: Non sticky service gets recreated (indefinetly?)

If I have a simple service that returns START_NOT_STICKY in onStartCommand
and I both start it explicitly (startService) and bind to it (bindService)
in onCreate of my activity the service gets recreated after it has been
killed over and over again. I unbind the service in onPause of my activity.

Logcat shows that the service is scheduled for restart and is restarted
over and over again. The service is not shown as running after it's been
recreated in Settings -> Apps -> Running.

08-08 21:57:25.081 D/ExampleActivity(23715): onPause
08-08 22:11:44.480 W/ActivityManager(  306): Scheduling restart of crashed
service com.example.service/.MainService in 5000 ms
08-08 22:11:49.519 I/ActivityManager(  306): Start proc
com.example.strangeservice for service com.example.service/.MainService
08-08 22:11:49.597 D/ExampleService(24079): onCreate
08-08 22:27:38.551 I/ActivityManager(  306): No longer want
com.example.service (pid 24079): hidden #16
08-08 22:27:38.574 W/ActivityManager(  306): Scheduling restart of crashed
service com.example.service/.MainService in 5000ms
08-08 22:27:43.629 I/ActivityManager(  306): Start proc
com.example.strangeservice for service com.example.service/.MainService:
pid=24401 uid=10101 gids={1028}
08-08 22:27:43.785 D/ExampleService(24401): onCreate
08-08 22:49:59.871 I/ActivityManager(  306): Process com.example.service
(pid 24401) has died.
08-08 22:49:59.871 W/ActivityManager(  306): Scheduling restart of crashed
service com.example.service/.MainService in 5000ms
08-08 22:50:04.934 I/ActivityManager(  306): Start proc
com.example.strangeservice for service com.example.service/.MainService:
pid=24857 uid=10101 gids={1028}
08-08 22:50:05.051 D/ExampleService(24857): onCreate

I thought START_NOT_STICKY meant that once it gets killed by Android it
wont be restarted unless I explicitly start it again. Or does binding to a
service override that and make the service sticky?

I've been testing this on a Galaxy Nexus with stock Android 4.1.1.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dianne Hackborn  
View profile  
 More options Aug 8 2012, 8:54 pm
From: Dianne Hackborn <hack...@android.com>
Date: Wed, 8 Aug 2012 17:54:41 -0700
Local: Wed, Aug 8 2012 8:54 pm
Subject: Re: [android-developers] Non sticky service gets recreated (indefinetly?)

If you bind to it with BIND_AUTO_CREATE, you are saying you want the
service to be created as long as you are bound to it.

From the documentation:

A service can be both started and have connections bound to it. In such a
case, the system will keep the service running as long as either it is
started or there are one or more connections to it with the
Context.BIND_AUTO_CREATE flag.

http://developer.android.com/reference/android/app/Service.html

On Wed, Aug 8, 2012 at 1:52 PM, Johan Appelgren
<johan.appelg...@gmail.com>wrote:

--
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Johan Appelgren  
View profile  
 More options Aug 9 2012, 1:46 am
From: Johan Appelgren <johan.appelg...@gmail.com>
Date: Wed, 8 Aug 2012 22:46:16 -0700 (PDT)
Local: Thurs, Aug 9 2012 1:46 am
Subject: Re: [android-developers] Non sticky service gets recreated (indefinetly?)

So START_NOT_STICKY has no effect once I've bound to it even if I unbind? I
must explicitly use stopService if the service has ever been bound to even
if there are no more connection?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dianne Hackborn  
View profile  
 More options Aug 9 2012, 2:35 am
From: Dianne Hackborn <hack...@android.com>
Date: Wed, 8 Aug 2012 23:35:50 -0700
Local: Thurs, Aug 9 2012 2:35 am
Subject: Re: [android-developers] Non sticky service gets recreated (indefinetly?)

Binding and starting are completely orthogonal things.  You bind to a
service to establish a persistent connection to it.  You start a service to
have it run for some undefined amount of time separately from the caller.
 A service needs to remain created as long as it is being asked to do
either of these things.

If you don't want it to stay created, either don't bind to it when you
don't want it around, or don't use BIND_AUTO_CREATE so you aren't forcing
it to remain created while bound to it.

On Wed, Aug 8, 2012 at 10:46 PM, Johan Appelgren
<johan.appelg...@gmail.com>wrote:

--
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Johan Appelgren  
View profile  
 More options Aug 9 2012, 6:09 am
From: Johan Appelgren <johan.appelg...@gmail.com>
Date: Thu, 9 Aug 2012 03:09:43 -0700 (PDT)
Local: Thurs, Aug 9 2012 6:09 am
Subject: Re: [android-developers] Non sticky service gets recreated (indefinetly?)

Ok, thanks. But shouldn't it (non-sticky service with no active
connections, nothing is bound to it) show up in the Running Services view
when it has been restarted? Or is that only for sticky services?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dianne Hackborn  
View profile  
 More options Aug 9 2012, 1:15 pm
From: Dianne Hackborn <hack...@android.com>
Date: Thu, 9 Aug 2012 10:15:09 -0700
Local: Thurs, Aug 9 2012 1:15 pm
Subject: Re: [android-developers] Non sticky service gets recreated (indefinetly?)

Binding to a service can only make that service's process as important as
the process of the client binding to it.  If the client process is in the
background, it can't bring the service up beyond the background, so it
doesn't count as a running process but a cached process.

On Thu, Aug 9, 2012 at 3:09 AM, Johan Appelgren
<johan.appelg...@gmail.com>wrote:

--
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Johan Appelgren  
View profile  
 More options Aug 10 2012, 3:17 am
From: Johan Appelgren <johan.appelg...@gmail.com>
Date: Fri, 10 Aug 2012 00:17:08 -0700 (PDT)
Local: Fri, Aug 10 2012 3:17 am
Subject: Re: [android-developers] Non sticky service gets recreated (indefinetly?)

Still occupies some amount of memory though, and the service's onCreate
might not be cheap.

Anyways, I guess this is one of those little undocumented things you just
have to learn. Do not rely on START_NOT_STICKY if you both start and bind
to a service if you don't want the service to stay around forever as a
cached process after you've unbound it.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dianne Hackborn  
View profile  
 More options Aug 10 2012, 2:39 pm
From: Dianne Hackborn <hack...@android.com>
Date: Fri, 10 Aug 2012 11:39:46 -0700
Local: Fri, Aug 10 2012 2:39 pm
Subject: Re: [android-developers] Non sticky service gets recreated (indefinetly?)

On Fri, Aug 10, 2012 at 12:17 AM, Johan Appelgren <johan.appelg...@gmail.com

> wrote:
> Still occupies some amount of memory though, and the service's onCreate
> might not be cheap.

I'm not sure what this means...?  If you are concerned about the service
having too much overhead because you have bound to it and it doesn't need
to run just while bound, don't use BIND_AUTO_CREATE.

> Anyways, I guess this is one of those little undocumented things you just
> have to learn. Do not rely on START_NOT_STICKY if you both start and bind
> to a service if you don't want the service to stay around forever as a
> cached process after you've unbound it.

Honestly I think it is pretty fully documented.  But maybe we have a
misunderstanding -- once you unbind from the service, the fact that you had
previously bound to it has no further impact on how it is handled, and if
its process is killed after that it will not be restarted.

As long as you are bound to it with BIND_AUTO_CREATE, the system will try
to keep it created and if the process is killed while you remain bound to
it then it will try to restart it.

As long as the service is in the started state, the system will try to keep
it created/started, and if the process is killed then it will be restarted
if it is not sticky.

The decision about whether to restart the service is if either of those
conditions result in it wanting to restart it.

Again starting and binding are orthogonal to each other.  You just have to
know how each works individually, and what happens in the service is based
on whether either of them drive it to need to be created.  As I think is
covered pretty fully in the documentation.

--
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kostya Vasilyev  
View profile  
 More options Aug 10 2012, 3:08 pm
From: Kostya Vasilyev <kmans...@gmail.com>
Date: Fri, 10 Aug 2012 23:08:37 +0400
Local: Fri, Aug 10 2012 3:08 pm
Subject: Re: [android-developers] Non sticky service gets recreated (indefinetly?)

2012/8/10 Dianne Hackborn <hack...@android.com>

> As long as the service is in the started state, the system will try to
> keep it created/started, and if the process is killed then it will be
> restarted if it is not sticky.

Is that a typo?

Was that supposed to be "Restarted if it *is* sticky"?

-- K


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Johan Appelgren  
View profile  
 More options Aug 10 2012, 5:26 pm
From: Johan Appelgren <johan.appelg...@gmail.com>
Date: Fri, 10 Aug 2012 14:26:46 -0700 (PDT)
Local: Fri, Aug 10 2012 5:26 pm
Subject: Re: [android-developers] Non sticky service gets recreated (indefinetly?)

You're right that I'm not understanding something here, most likely it is
something really obvious too. :(

As I wrote, my understanding of what you describe is not the behavior I'm
seeing with my test app on my Galaxy Nexus with Android 4.1.1 nor the 4.1.1
emulator image. After I've unbound from the service, in my test I do this
in the activity's onPause, when the app process is killed, it and the
service is restarted. Then after that the process and service is killed and
restarted every now and then, without starting the activity or any calls to
startService or bindService made by any code in my test app. It starts
quicker if I start a couple of different games to put some memory pressure
on the system.

Not even stopping the cached process in the Cached processes list stops it,
it is restarted a little while after. Only going to the Downloaded list and
pressing the Force stop button stops the stop/restart cycle.

Perhaps someone could look at my dummy app code and point out what I'm
doing wrong. If someone does, I'm sorry for most likely wasting your time.

public class MainActivity extends Activity {

@SuppressWarnings("unused")
private int[] mDummyData = new int[3 * 1024 * 1024];
 private final String TAG = "MainActivity";
 private boolean mBound;
private boolean mCalledBind;
private boolean mStarted;
    private final ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className, IBinder
service) {
        Log.d(TAG, "onServiceConnected");
            mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
        Log.d(TAG, "onServiceDisconnected");
        }
    };

    public MainActivity(){
    Log.d(TAG, "ctor");
    }    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(this, MainService.class);

        Log.d(TAG, "onCreate");

        if (!mStarted) {
        Log.d(TAG, "startService");
        startService(intent);
        mStarted = true;
        }

        if (!mBound && !mCalledBind) {
        Log.d(TAG, "bindService");
            bindService(intent, mConnection, BIND_AUTO_CREATE);
            mCalledBind = true;
        }
    }

    @Override
    protected void onPause() {
    Log.d(TAG, "onPause");

        if (mBound) {
        Log.d(TAG, "unbindService");
            unbindService(mConnection);
            mBound = false;
            mCalledBind = false;
        }

    super.onPause();
    }

    @Override
    protected void onDestroy() {
    Log.d(TAG, "onDestroy");

    super.onDestroy();
    }

}

public class MainService extends Service {

@SuppressWarnings("unused")
private int[] mDummyData = new int[1024*1024];
 private static final String TAG = "MainService";
private final IBinder mBinder = new LocalBinder();
 public class LocalBinder extends Binder {
MainService getService() {
            return MainService.this;
        }
    }
 @Override
public IBinder onBind(Intent arg0) {
Log.d(TAG, "onBind");
return mBinder;

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
return START_NOT_STICKY;
}

 @Override
public void onCreate() {
Log.d(TAG, "onCreate");
super.onCreate();


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dianne Hackborn  
View profile  
 More options Aug 11 2012, 2:17 pm
From: Dianne Hackborn <hack...@android.com>
Date: Sat, 11 Aug 2012 11:17:06 -0700
Local: Sat, Aug 11 2012 2:17 pm
Subject: Re: [android-developers] Non sticky service gets recreated (indefinetly?)

Sorry yes I meant if it *is* sticky.

On Fri, Aug 10, 2012 at 12:08 PM, Kostya Vasilyev <kmans...@gmail.com>wrote:

--
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dianne Hackborn  
View profile  
 More options Aug 11 2012, 2:21 pm
From: Dianne Hackborn <hack...@android.com>
Date: Sat, 11 Aug 2012 11:21:07 -0700
Local: Sat, Aug 11 2012 2:21 pm
Subject: Re: [android-developers] Non sticky service gets recreated (indefinetly?)

Unless the service has something bound to it or it is started, it will not
be re-created.

You can look at the service state with "adb shell dumpsys activity
services" or "adb shell dumpsys activity service <package_name or
service_name>".  If your service is listed there, it will show you what its
current state is -- what is bound to it, whether it is started, etc.

On Fri, Aug 10, 2012 at 2:26 PM, Johan Appelgren
<johan.appelg...@gmail.com>wrote:

--
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dianne Hackborn  
View profile  
 More options Aug 11 2012, 2:22 pm
From: Dianne Hackborn <hack...@android.com>
Date: Sat, 11 Aug 2012 11:22:55 -0700
Local: Sat, Aug 11 2012 2:22 pm
Subject: Re: [android-developers] Non sticky service gets recreated (indefinetly?)

Also, if the service is not showing up as a running service in manage apps,
then that should mean that it is not started, so it it is created that is
because something has bound to it.  You can see what is bound to it with
those shell commands.  If it is not started, the only reason it would be
recreated is because another process continue to be bound to it after its
process is killed.

On Sat, Aug 11, 2012 at 11:21 AM, Dianne Hackborn <hack...@android.com>wrote:

--
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Johan Appelgren  
View profile  
 More options Aug 12 2012, 4:50 pm
From: Johan Appelgren <johan.appelg...@gmail.com>
Date: Sun, 12 Aug 2012 13:50:10 -0700 (PDT)
Local: Sun, Aug 12 2012 4:50 pm
Subject: Re: [android-developers] Non sticky service gets recreated (indefinetly?)

So it looks like I'm missing something about how to unbind from a service.
I thought that calling unbindService with the same ServiceConnection
instance would be enough. Looking at the output from dumpsys it looks only
partially unbound or something. I guess this explains why it is restarted.
How do I completely unbind?

>adb shell dumpsys activity services com.example.service

ACTIVITY MANAGER SERVICES (dumpsys activity services)
  Active services:
  * ServiceRecord{426c09c0 com.example.service/.MainService}
    intent={cmp=com.example.service/.MainService}
    packageName=com.example.service
    processName=com.example.service
    baseDir=/data/app/com.example.service-1.apk
    dataDir=/data/data/com.example.service
    app=ProcessRecord{42183390 8027:com.example.service/u0a97}
    createTime=-1m2s860ms lastActivity=-1m2s857ms
    executingStart=-3s22ms restartTime=-1m2s860ms
    startRequested=true stopIfKilled=true callStart=true lastStartId=1
    Bindings:
    * IntentBindRecord{426dda38}:
      intent={cmp=com.example.service/.MainService}
      binder=android.os.BinderProxy@42610298
      requested=true received=true hasBound=false doRebind=false


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eugene Katsevman  
View profile  
 More options Apr 20, 4:14 am
From: Eugene Katsevman <eugene.katsev...@gmail.com>
Date: Sat, 20 Apr 2013 01:14:07 -0700 (PDT)
Local: Sat, Apr 20 2013 4:14 am
Subject: Re: [android-developers] Non sticky service gets recreated (indefinetly?)

Hello!
I've got some more strange results on nexus 7 with android 4.2.2  If you
just start a NOT_STICKY service and then kill your app by swiping it off
from recents, a message in logcat appears

W/ActivityManager(489): Scheduling restart of crashed service
com.example.mediaservicetest/.MediaService in 5000ms

but no restart really happens.
I've unsure if it is an intended behaviour of android system. Some help
could be very-very useful and appreciated.

понедельник, 13 августа 2012 г., 0:50:10 UTC+4 пользователь Johan Appelgren
написал:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »