Using startForeground() with an Intent Service

后端 未结 3 1724
长发绾君心
长发绾君心 2020-12-16 07:26

I am trying to keep alive a service that reacts to screen on/off changes. The service would work perfectly for awhile, but then eventually it would be killed. I am now attem

相关标签:
3条回答
  • 2020-12-16 07:50

    (Updated) I suppose there are the following possible cases:

    1) documentation for IntentService states:

    the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.

    So, it might be that your service is normally stopped after onHandleIntent() is finished (especially, as you mentioned that startForeground() added no extra life to the process).

    2) You might try to check if it's somehow can be related to device going to sleep (or maybe you are starting your service by schedule and awkening device - in this case you might need to acquire WakeLock)

    3) In the very rare cases, the system still can kill foreground process - so if you do a lot of allocations (really lot) and some other work in onHandleIntent() (instead of "Blah Blah Blah" at your code) - you might run into it - but I suppose it's not the case.

    As question's title is "Using startForeground() with an IntentService" - would like to clarify that too: I believe nothing (architecture, best practices, android framework, java docs for IntentService) prevents you from running your intent service as a foreground. Of course you need to thought out carefully its usage and whether you actually need a foreground service. Some ideas are available here. For sample code see below. (Sample code can end up showing multiple notifications if you queued multiple jobs/intents into IntentService, so there might be better solution depending on your need.)


    public class ForegroundService extends IntentService {
    
        private static final String TAG = "FrgrndSrv";
    
        public ForegroundService() {
            super(TAG);
        }
    
        @Override
        protected void onHandleIntent(Intent intent) {
    
            Notification.Builder builder = new Notification.Builder(getBaseContext())
                    .setSmallIcon(R.drawable.ic_foreground_service)
                    .setTicker("Your Ticker") // use something from something from R.string
                    .setContentTitle("Your content title") // use something from something from
                    .setContentText("Your content text") // use something from something from
                    .setProgress(0, 0, true); // display indeterminate progress
    
            startForeground(1, builder.build());
            try {
                doIntesiveWork();
            } finally {
                stopForeground(true);
            }
        }
    
        protected void doIntesiveWork() {
            // Below should be your logic that takes lots of time
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-16 08:00

    If memory is low and your consuming too much memory and your sitting in the background for too long then you WILL be killed by the ActivityManager.

    0 讨论(0)
  • 2020-12-16 08:01

    IntentService automatically shuts down when onHandleIntent() completes. It is to perform a brief bit of work when something occurs. It is not supposed to live for more than a few seconds, typically.

    I am going to assume that this is tied to what I wrote in your last question in this area.


    Something in the rest of your app will be registering and unregistering the BroadcastReceiver for the screen on/off events -- apparently, from your comments, it is an activity. If what you want to do when those things occur is very very quick (on the order of a few milliseconds), just do the work in onReceive(), and be done with it.

    If, on the other hand, you have more work than a few milliseconds' worth, you will need to have that work be done by something else that can do the work on a background thread. For example, if the "something in the rest of your app" that registered the BroadcastReceiver is indeed an activity, the activity might just spawn an AsyncTask to do the work.

    Another possibility is to use an IntentService. You elected to go down this path in your work prior to that last question. I do not know why. Regardless, an IntentService, like an AsyncTask, is supposed to be a short-lived component -- you send it a command via startService(), it does its work in onHandleIntent(), and it goes away.

    With all that in mind, let's talk about your specific points.


    The service would work perfectly for awhile, but then eventually it would be killed.

    It is unclear what you think "killed" means. An IntentService automatically goes away once onHandleIntent() returns, and that ideally should occur within a handful of seconds.

    I am now attempting to use startForeground() to keep the process alive, but it still seems to be dying.

    Again, it is unclear what you think "dying" means. Bear in mind that the mere existence of an IntentService does not stop the CPU from shutting down once the screen turns off, and startForeground() has nothing to do with that.

    Also, as a side note, Logcat complains about a leak, as unregisterReceiver() is not called (except manually by a button press from the user)..

    You also need to unregister the receiver before the user exits the activity. It is usually a good idea to call registerReceiver() in onResume() and unregisterReceiver() in onPause().

    0 讨论(0)
提交回复
热议问题