1

I developed an app which should remind me at a certain time. Therefore I implemented an IntentService which starts a notification. The problem is that the notification will be created while the app is in foreground or at least in background open. If I close the app using the task manager the notification is no longer running.

Am I using the wrong service? Or do I have to create something else?

in the intentservice class:

private static final String serviceName = "sebspr.de.deadlines.DeadLineService";

public DeadLineService() {
    super(serviceName);
}

@Override
protected void onHandleIntent(Intent intent) {

    Context ctx = getApplicationContext();
    Intent intent = new Intent(ctx, DeadLineService.class);
    PendingIntent pIntent = PendingIntent.getActivity(ctx, 0, intent, 0);


    String title = getTitle(list.size());
    String text = getText(list);
    Notification noti = new Notification.Builder(ctx)
            .setContentTitle(title)
            .setContentText(text).setSmallIcon(R.mipmap.ic_notfi)
            .setContentIntent(pIntent)
            .build();
    NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    // hide the notification after its selected
    noti.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, noti);



}

int the MainActivity:

Intent msgIntent = new Intent(this, DeadLineService.class);
startService(msgIntent);

UPDATE

Considering my actual problem the solution is, to take a look at the AlarmManager. The IntentService is here the wrong way to go. I found a good tutorial here: AlarmManger Example

BGreene
  • 169
  • 1
  • 15
Bolic
  • 705
  • 2
  • 12
  • 30
  • And in your opinion if app closed via task manager it still should show notification or ..? – Divers May 18 '15 at 17:19
  • You kill the app in task manager, it'll delete the notification because you killed the app. That's precisely what is supposed to happen. – DeeV May 18 '15 at 17:19
  • Okay. But I want that the user still gets the notification. How can I handle this? – Bolic May 18 '15 at 17:26

2 Answers2

2

You shouldn't worry about a user killing the app via the task manager. If that happens, you really shouldn't start back up automatically. It isn't a user-friendly way to do things.

What you should worry about is a system restart in which case you can register your app to be notified when the device turns on.

Check out this answer for more details on starting your service when the phone turns on.

Also start your service sticky

@Override
public int onStartCommand(Intent intent, int flags, int startId) { 
    return START_STICKY;
}
Community
  • 1
  • 1
Jon
  • 9,156
  • 9
  • 56
  • 73
1

Actually u can try to keep your application alive after killed but it may annoy your users a bit... So, Look out...

How to save Alarm after app killing?

Community
  • 1
  • 1
eduyayo
  • 2,020
  • 2
  • 15
  • 35