StartForeground for IntentService

前端 未结 2 705
抹茶落季
抹茶落季 2020-12-15 23:35

I have an IntentService and I want to make it sticky with an ongoing notification. The problem is that the notification appears and then disappears immediately.

相关标签:
2条回答
  • 2020-12-15 23:49

    This should not be an IntentService. As written, your IntentService will live for a millisecond or so. Once onHandleIntent() returns, the service is destroyed. This should be a regular Service, where you fork your own thread and manage the lifetime of the thread and the service.

    The reason your Notification is going away immediately is because the service is going away immediately.

    0 讨论(0)
  • 2020-12-16 00:02

    As 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, I suppose the problem, is that your service is out of work after onHandleIntent() is finished. Hence, service stops itself and notification is dissmissed. So, concept of IntentService is probably not the best case for your task.


    As question's title is "StartForeground for IntentService", I would like to clarify some things:

    It's really simple to make your IntentService to run in foreground (see code below), but for sure you need to consider several things:

    • Don't run service in foreground if it takes a few seconds only - that might bug your users. Imagine you run short tasks periodically - that will cause notification to appear and disappear - uhhhh*

    • You might need to make your service capable to keep device awake (but that's another story, which is covered pretty well on stackoverflow)*

    • If you queued up multiple Intents to your IntentService the below code will end up showing/hiding notification. (So there might be better solution for your case - as @CommonsWare suggests to extend Service and do everything yourself, however would like to mention - there is nothing in javadoc for IntentService saying that it works only a few seconds - it works as long as it has to do something.)


    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)
提交回复
热议问题