1

Because someone recommended I implemented an IntentService to do some work in the background. For now i just implemented a very basic service with some dummy code to pretend some long running work:

public class ImageSendEmailService extends IntentService {
    private static final int MY_NOTIFICATION_ID = 1;
    private NotificationManager notificationManager = null;
    private Notification notification = null;

    public ImageSendEmailService() {
        super("EmailService");
    }

    @Override
    public void onCreate() {
        super.onCreate();

        this.notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        for (int i = 0; i <= 10; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            String notificationText = String.valueOf((int) (100 * i / 10)) + " %";

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
            builder.setContentTitle("Progress");
            builder.setContentText(notificationText);
            builder.setTicker("Notification!");
            builder.setWhen(System.currentTimeMillis());
            builder.setDefaults(Notification.DEFAULT_SOUND);
            builder.setAutoCancel(true);
            builder.setSmallIcon(R.drawable.ic_launcher);

            this.notification = builder.build();
            this.notificationManager.notify(MY_NOTIFICATION_ID, this.notification);
        }
    }
}

Unfortunately the ui process in stopping always when i kill the app. If the progress for example is at 50% and i kill the app the progress stays at 50% and does not continue. The documentation says that an IntentService is not getting killed until its work is done but it gets killed in my case.

Later the IntentService should be used for several tasks:

  1. Sending Images using E-Mail
  2. Storing Images on a Server
  3. Repeating the Task automatically when the Task failed caused by missing internet connection.

It is also important to run in the background because i dont want the task to get interrupted when the user gets a phone call. and the repetition of the task is even more important. there could be temporally no connection to the internet, low battery status or even a crash of the whole phone.

Mulgard
  • 9,877
  • 34
  • 129
  • 232

1 Answers1

0

The documentation says that an IntentService is not getting killed until its work is done but it gets killed in my case.

I've checked the documentation and it says nothing about such behavior, so I think you have a little misunderstanding here. Your IntentService is the application's component. If you kill your app then all of its components are killed.

aga
  • 27,954
  • 13
  • 86
  • 121
  • The comments from this post here: http://stackoverflow.com/questions/27205134/service-and-asynctask?noredirect=1#comment42893791_27205134 are all saying that an intentservice should be alive as long as it has work to do. if not, what should i do instead? – Mulgard Nov 29 '14 at 19:37
  • To summarise: You should start the `Service` in the foreground, this automatically bundles it with a notification which cannot be dismissed and keeps it from being killed by the OS for example when the memory is low. Additionally you can specify that the `Service` should run in it's own process. Although in my experience that should not be necessary. If you look at the behaviour of other apps regarding foreground `Services` like the Google Play Store and Google Play Music the `Service` itself always seems to survive the process being killed, but the associated UI does not. – Xaver Kapeller Nov 29 '14 at 20:02
  • So now i should use a `Service` but my intention didnt change... some hours ago you recommended me to use an `IntentService` – Mulgard Nov 29 '14 at 20:06
  • @Mulgard I just explained to you your options, their upsides and their downsides. If you just want to process data in a `Service` asynchronously than using an `IntentService` is very convenient and recommended. If you want to show a progress in a notification as this work is being done and you want to prevent that `Service` from being killed during normal operation than starting the `Service` in the foreground would be best practice. But it all depends on what you want to do and your requirements. – Xaver Kapeller Nov 29 '14 at 20:11
  • I guess i listed my requirements quite well. I will try this now: http://www.truiton.com/2014/10/android-foreground-service-example/ – Mulgard Nov 29 '14 at 20:14