Non sticky service gets recreated (indefinetly?)

Showing 1-16 of 16 messages
Non sticky service gets recreated (indefinetly?) Johan Appelgren 8/8/12 1:52 PM
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.

Re: [android-developers] Non sticky service gets recreated (indefinetly?) Dianne Hackborn 8/8/12 5:54 PM
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.



--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-d...@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en



--
Dianne Hackborn
Android framework engineer
hac...@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.

Re: [android-developers] Non sticky service gets recreated (indefinetly?) Johan Appelgren 8/8/12 10:46 PM
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? 
Re: [android-developers] Non sticky service gets recreated (indefinetly?) Dianne Hackborn 8/8/12 11:35 PM
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.
Re: [android-developers] Non sticky service gets recreated (indefinetly?) Johan Appelgren 8/9/12 3:09 AM
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?
Re: [android-developers] Non sticky service gets recreated (indefinetly?) Dianne Hackborn 8/9/12 10:15 AM
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.
Re: [android-developers] Non sticky service gets recreated (indefinetly?) Johan Appelgren 8/10/12 12:17 AM
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. 
Re: [android-developers] Non sticky service gets recreated (indefinetly?) Dianne Hackborn 8/10/12 11:39 AM
On Fri, Aug 10, 2012 at 12:17 AM, Johan Appelgren <johan.a...@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.

Re: [android-developers] Non sticky service gets recreated (indefinetly?) Kostya Vasilyev 8/10/12 12:08 PM

2012/8/10 Dianne Hackborn <hac...@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

Re: [android-developers] Non sticky service gets recreated (indefinetly?) Johan Appelgren 8/10/12 2:26 PM
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();
}
}
Re: [android-developers] Non sticky service gets recreated (indefinetly?) Dianne Hackborn 8/11/12 11:17 AM
Sorry yes I meant if it *is* sticky.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-d...@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Non sticky service gets recreated (indefinetly?) Dianne Hackborn 8/11/12 11:21 AM
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.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-d...@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en



--
Dianne Hackborn
Android framework engineer
hac...@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.

Re: [android-developers] Non sticky service gets recreated (indefinetly?) Dianne Hackborn 8/11/12 11:22 AM
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.
Re: [android-developers] Non sticky service gets recreated (indefinetly?) Johan Appelgren 8/12/12 1:50 PM
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
unk...@googlegroups.com 4/19/13 1:08 AM <This message has been deleted.>
Re: [android-developers] Non sticky service gets recreated (indefinetly?) Eugene Katsevman 4/20/13 1:14 AM
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 написал:
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?