StartForeground for IntentService

I have an IntentService and I want to make it sticky with the current notification. The problem is that the notification appears and then disappears immediately. The service continues to work. How to use startForeground in IntentService?

@Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); Notification notification = new Notification(R.drawable.marker, "Notification service is running", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, DashboardActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "App", "Notification service is running", pendingIntent); notification.flags|=Notification.FLAG_NO_CLEAR; startForeground(1337, notification); return START_STICKY; } @Override protected void onHandleIntent(Intent intent) { String id = intent.getStringExtra(ID); WebSocketConnectConfig config = new WebSocketConnectConfig(); try { config.setUrl(new URI("ws://" + App.NET_ADDRESS + "/App/socket?id="+id)); } catch (URISyntaxException e) { e.printStackTrace(); } ws = SimpleSocketFactory.create(config, this); ws.open(); } 

thanks

+7
source share
2 answers

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

The reason your Notification leaves immediately is because the service is leaving immediately.

+9
source

As the documentation for IntentService states:

... the service starts as needed, processes each intention every time using the workflow, and stops when the work finishes.

So, I suppose the problem is that your service does not work after onHandleIntent() . Consequently, the service stops and the notification is interrupted. So the concept of IntentService is probably not the best option for your task.


As the title of the question is “StartForeground for IntentService”, I would like to clarify some things:

It is very simple to make IntentService run in the foreground (see the code below), but for sure you need to consider a few things:

  • Do not run the service in the foreground if it takes only a few seconds - this may lead to errors for your users. Imagine that you periodically perform short tasks - this will lead to the appearance and disappearance of a notification - uhhhh *

  • You may need to make your service able to keep the device up and running (but this is another story that covers stackoverflow pretty well) *

  • If you queued several intentions in your IntentService, the code below will show / hide the notification. (Thus, there may be a better solution for your case - since @CommonsWare offers to expand the Service and do everything yourself, however, I would like to mention that there is nothing in IntaService that would mean that it only works for a few seconds - it works until while he 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(); } } } 
+1
source

All Articles